Jump to content
Sign in to follow this  
JoshDB

Cleaning/optimizing Code

Recommended Posts

JoshDB

Hey guys, just wanted to know if there was a better way of doing what I'm trying to achieve. This is a launcher for a total conversion mod for the game TES: IV Oblivion.

First, Install.exe is launched, which unpacks the appropriate files. It then moves exe's around to ensure that when you're playing the TC, you get the TC files; When you play Oblivion, you get the Oblivion files.

#include "Zip.au3"

DirCreate(@WorkingDir & "\DawnFiles")

FileInstall("C:\Program Files\Bethesda Softworks\Oblivion\Dawn\Dist\DawnInstallPackage.zip",@WorkingDir & "\Data\DawnFiles\DawnInstallPackage.zip")
FileInstall("XZip.dll","XZip.dll")
_ZipOpen(@WorkingDir & "\Data\DawnFiles\DawnInstallPackage.zip")
_ZipUnpack(@WorkingDir & "\Data\DawnFiles")
_ZipClose()
FileDelete(@WorkingDir & "\Data\DawnFiles\DawnInstallPackage.zip")

FileMove("OblivionLauncher.exe","OblivionLauncher_Original.exe")
FileInstall("OblivionLauncher_Patched.exe","OblivionLauncher.exe")
FileInstall("Dawn.exe","Dawn.exe")
FileInstall("DawnIcon.ico","DawnIcon.ico")
FileInstall("OblivionIcon.ico","OblivionIcon.ico")

FileCreateShortcut(@WorkingDir & "\Dawn.exe",@DesktopDir & "\Dawn.lnk",@WorkingDir,"", "Dawn: A Red Sun Rises", @WorkingDir & "\DawnIcon.ico")

Whenever Dawn is launched from the desktop, this code runs:

If NOT FileExists(@WorkingDir & "\Data\OriginalFiles") Then
    CleanDirectory()
    If FileExists(@WorkingDir & "\Data\DawnFiles") Then
        PrepareFiles()
    EndIf
EndIf

Func PrepareFiles()
    DirMove(@WorkingDir & "\Data\DawnFiles\Meshes",@WorkingDir & "\Data",1)
    DirMove(@WorkingDir & "\Data\DawnFiles\Textures",@WorkingDir & "\Data",1)
    FileMove(@WorkingDir & "\Data\DawnFiles\Dawn.esp",@WorkingDir & "\Data\Dawn.esp",1)
    DirRemove(@WorkingDir & "\Data\DawnFiles")
EndFunc

Func CleanDirectory()
    DirCreate(@WorkingDir & "\Data\OriginalFiles")
    
    DirMove(@WorkingDir & "\Data\Meshes",@WorkingDir & "\Data\OriginalFiles\Meshes")
    DirMove(@WorkingDir & "\Data\Textures",@WorkingDir & "\Data\OriginalFiles\Textures")
    DirMove(@WorkingDir & "\Data\Menus",@WorkingDir & "\Data\OriginalFiles\Menus")
    DirMove(@WorkingDir & "\Data\Sound",@WorkingDir & "\Data\OriginalFiles\Sound")
    DirMove(@WorkingDir & "\Data\Method A",@WorkingDir & "\Data\OriginalFiles\Method A")
    DirMove(@WorkingDir & "\Data\Method B",@WorkingDir & "\Data\OriginalFiles\Method B")
    
    BackupType(".esp")
    BackupType(".esm")
    BackupType(".nif")
    BackupType(".txt")
    BackupType(".cfg")
    BackupType(".exe")
EndFunc

Func BackupType($Type = ".esp")
    $Files = FileFindFirstFile("Data\*" & $Type)
    
    If $Files = -1 Then
    Else
        $Count = IniRead(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index","Master",0)
        
        While 1
            $Count = $Count + 1
            $CurrentFile = FileFindNextFile($Files) 
            If @error Then ExitLoop
            
            IniWrite(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index","Master",IniRead(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index","Master",0) + 1)
            IniWrite(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index",$Count & "a",@WorkingDir & "\Data\" & $CurrentFile)
            IniWrite(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index",$Count & "b",@WorkingDir & "\Data\OriginalFiles\" & $CurrentFile)
            
            FileMove(@WorkingDir & "\Data\" & $CurrentFile,@WorkingDir & "\Data\OriginalFiles\" & $CurrentFile)
        WEnd
    EndIf
    
    FileClose($Files)
EndFunc

Run("Oblivion.exe")

Whenever Oblivion is launched from the desktop, this code runs:

If FileExists(@WorkingDir & "\Data\OriginalFiles") Then
    If NOT FileExists(@WorkingDir & "\Data\DawnFiles") Then
        CleanFiles()
    EndIf
    ReturnDirectory()
EndIf

Func CleanFiles()
    DirCreate(@WorkingDir & "\Data\DawnFiles")
    FileMove(@WorkingDir & "\Data\Dawn.esp",@WorkingDir & "\Data\DawnFiles\Dawn.esp")
    DirMove(@WorkingDir & "\Data\Meshes",@WorkingDir & "\Data\DawnFiles\Meshes")
    DirMove(@WorkingDir & "\Data\Textures",@WorkingDir & "\Data\DawnFiles\Textures")
EndFunc

Func ReturnDirectory()
    $LoopCount = IniRead(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index","Master",0)
    
    DirMove(@WorkingDir & "\Data\OriginalFiles\Meshes",@WorkingDir & "\Data\Meshes")
    DirMove(@WorkingDir & "\Data\OriginalFiles\Textures",@WorkingDir & "\Data\Textures")
    DirMove(@WorkingDir & "\Data\OriginalFiles\Menus",@WorkingDir & "\Data\Menus")
    DirMove(@WorkingDir & "\Data\OriginalFiles\Sound",@WorkingDir & "\Data\Sound")
    DirMove(@WorkingDir & "\Data\OriginalFiles\Method A",@WorkingDir & "\Data\Method A")
    DirMove(@WorkingDir & "\Data\OriginalFiles\Method B",@WorkingDir & "\Data\Method B")
    
    For $LoopNumber = 0 To $LoopCount
        FileMove(IniRead(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index",$LoopNumber & "b",0),IniRead(@WorkingDir & "\Data\OriginalFiles\BackupIndex.ini","Index",$LoopNumber & "a",0))
    Next
    
    $BackupSize = DirGetSize(@WorkingDir & "\Data\OriginalFiles\")
    
    If Round($BackupSize / 1024) < 150 Then 
        DirRemove(@WorkingDir & "\Data\OriginalFiles\",1)
    EndIf
EndFunc

Run("OblivionLauncher_Original.exe")

This all runs great, but does anyone have any suggestions?

Edited by JoshDB

Ha, I haven't been on these forums since... 2006, almost. Behold, my legacy signature:My AutoIt idol is Valuater. You know you love him, too.My Stuff: D&D AGoT Tools Suite

Share this post


Link to post
Share on other sites
Neoborn

Bueller? Bueller? Some other posts get instant replies maybe this one is too much like hard work :)


~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites
Valuater

quick observation...

the iniwrite files look "over-done", same with moving all of the files, however there could be reasoning for doing this

and... this doesnt look correct

Func PrepareFiles()

DirMove(@WorkingDir & "\Data\DawnFiles\Meshes",@WorkingDir & "\Data",1)

DirMove(@WorkingDir & "\Data\DawnFiles\Textures",@WorkingDir & "\Data",1)

FileMove(@WorkingDir & "\Data\DawnFiles\Dawn.esp",@WorkingDir & "\Data",1)

DirRemove(@WorkingDir & "\Data\DawnFiles")

EndFunc

the destination does not have a file extention...ie ".esp".. OR a closing folder slash.. "\"

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
JoshDB

quick observation...

the iniwrite files look "over-done", same with moving all of the files, however there could be reasoning for doing this

and... this doesnt look correct

Func PrepareFiles()

DirMove(@WorkingDir & "\Data\DawnFiles\Meshes",@WorkingDir & "\Data",1)

DirMove(@WorkingDir & "\Data\DawnFiles\Textures",@WorkingDir & "\Data",1)

FileMove(@WorkingDir & "\Data\DawnFiles\Dawn.esp",@WorkingDir & "\Data",1)

DirRemove(@WorkingDir & "\Data\DawnFiles")

EndFunc

the destination does not have a file extention...ie ".esp".. OR a closing folder slash.. "\"

8)

This script moves almost everything in C:\Program Files\Bethesda Softworks\Oblivion\Data to Data\OriginalFiles, and moves the contents of Data\DawnFiles to Data. The IniWrites are basically a way of tabbing which files have been moved, and where to put them back whenever they're restored.

Thanks for catching that filemove, I think I just copied the previous commands and then changed the first param :)


Ha, I haven't been on these forums since... 2006, almost. Behold, my legacy signature:My AutoIt idol is Valuater. You know you love him, too.My Stuff: D&D AGoT Tools Suite

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×