aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfusion32 <marcopuzziello@gmail.com>2025-05-22 23:17:54 -0300
committerfusion32 <marcopuzziello@gmail.com>2025-05-22 23:17:54 -0300
commitb7432b4e74284138f1740f4577b9cb32e1f120d0 (patch)
tree055cdc4653396996ab13ced99198338f906294dc
parent4e41f79a50b18b4e9b9fa00bbfa68ecb15fcb63e (diff)
downloadgame-b7432b4e74284138f1740f4577b9cb32e1f120d0.tar.gz
game-b7432b4e74284138f1740f4577b9cb32e1f120d0.zip
implement `SaveFile`
-rw-r--r--TODO.md1
-rw-r--r--reference/game.c124
-rw-r--r--src/script.cc55
3 files changed, 55 insertions, 125 deletions
diff --git a/TODO.md b/TODO.md
index 992d0a0..bf2c2ae 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,5 +1,4 @@
## TODO NEXT
-- SaveFile ??
- TCreature
- TPlayer
- TNonPlayer
diff --git a/reference/game.c b/reference/game.c
index 1ce2aba..1df63aa 100644
--- a/reference/game.c
+++ b/reference/game.c
@@ -2653,130 +2653,6 @@ void UnpackRelativeCoordinate(void)
return;
}
-void SaveFile(char *Filename)
-
-{
- int iVar1;
- FILE *pFVar2;
- FILE *__stream;
- size_t sVar3;
- size_t sVar4;
- int iVar5;
- int *piVar6;
- size_t Size;
- FILE *Source;
- undefined4 uStack_1040;
- char *pcVar7;
- undefined4 uStack_1030;
- undefined1 auStack_102c [4];
- undefined1 *local_1028;
- uchar_0_ *Buffer;
- FILE *local_1020;
- FILE *Dest;
- char BackupFilename [4096];
-
- Buffer = &stack0xffffefc4;
- if (Filename == (char *)0x0) {
- uStack_1040 = 0x804ec01;
- error("SaveFile: Filename ist NULL.\n");
- return;
- }
- uStack_1040 = 0x804ea52;
- Buffer = &stack0xffffefc4;
- strcpy((char *)&Dest,Filename);
- uStack_1040 = 0x804ea62;
- strcat((char *)&Dest,"#");
- uStack_1040 = 0x804ea75;
- __stream = fopen(Filename,"rb");
- if (__stream == (FILE *)0x0) {
- pcVar7 = "SaveFile: Quelldatei %s existiert nicht.\n";
-LAB_0804ebdb:
- uStack_1040 = 0x804ebe0;
- error(pcVar7);
- return;
- }
- uStack_1040 = 0x804ea8f;
- local_1020 = fopen((char *)&Dest,"wb");
- if (local_1020 == (FILE *)0x0) {
- pcVar7 = "SaveFile: Kann Zieldatei %s nicht anlegen.\n";
- local_1020 = (FILE *)0x0;
- goto LAB_0804ebdb;
- }
- uStack_1040 = 0x804eab5;
- fseek(__stream,0,2);
- uStack_1040 = 0x804eabd;
- sVar3 = ftell(__stream);
- uStack_1040 = 0x804ead7;
- fseek(__stream,0,0);
- iVar1 = -(sVar3 + 0xf & 0xfffffff0);
- local_1028 = auStack_102c + iVar1;
- *(FILE **)((int)&uStack_1030 + iVar1) = __stream;
- *(size_t *)(BackupFilename + iVar1 + -0x1c) = sVar3;
- pcVar7 = BackupFilename + iVar1 + -0x20;
- pcVar7[0] = '\x01';
- pcVar7[1] = '\0';
- pcVar7[2] = '\0';
- pcVar7[3] = '\0';
- *(undefined1 **)(&stack0xffffefc4 + iVar1) = auStack_102c + iVar1;
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb01;
- sVar4 = fread(*(void **)(&stack0xffffefc4 + iVar1),*(size_t *)(BackupFilename + iVar1 + -0x20),
- *(size_t *)(BackupFilename + iVar1 + -0x1c),*(FILE **)((int)&uStack_1030 + iVar1))
- ;
- pFVar2 = local_1020;
- if (sVar4 == sVar3) {
- pcVar7 = BackupFilename + iVar1 + -0x20;
- pcVar7[0] = '\x01';
- pcVar7[1] = '\0';
- pcVar7[2] = '\0';
- pcVar7[3] = '\0';
- *(size_t *)(BackupFilename + iVar1 + -0x1c) = sVar4;
- *(FILE **)((int)&uStack_1030 + iVar1) = pFVar2;
- *(undefined1 **)(&stack0xffffefc4 + iVar1) = local_1028;
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804ebb4;
- sVar3 = fwrite(*(void **)(&stack0xffffefc4 + iVar1),
- *(size_t *)(BackupFilename + iVar1 + -0x20),
- *(size_t *)(BackupFilename + iVar1 + -0x1c),
- *(FILE **)((int)&uStack_1030 + iVar1));
- if (sVar3 == sVar4) goto LAB_0804eb1e;
- *(char **)(&stack0xffffefc4 + iVar1) = "SaveFile: Fehler beim Schreiben der Zieldatei %s.\n"
- ;
- *(char **)(BackupFilename + iVar1 + -0x20) = Filename;
- }
- else {
- *(char **)(&stack0xffffefc4 + iVar1) = "SaveFile: Fehler beim Lesen der Quelldatei %s.\n";
- *(char **)(BackupFilename + iVar1 + -0x20) = Filename;
- }
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb1e;
- error(*(char **)(&stack0xffffefc4 + iVar1));
-LAB_0804eb1e:
- *(FILE **)(&stack0xffffefc4 + iVar1) = __stream;
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb26;
- iVar5 = fclose(*(FILE **)(&stack0xffffefc4 + iVar1));
- if (iVar5 != 0) {
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb75;
- piVar6 = __errno_location();
- iVar5 = *piVar6;
- *(undefined **)(&stack0xffffefc4 + iVar1) = &DAT_080ef3a0;
- *(int *)(BackupFilename + iVar1 + -0x20) = iVar5;
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb87;
- error(*(char **)(&stack0xffffefc4 + iVar1));
- }
- *(FILE **)(&stack0xffffefc4 + iVar1) = local_1020;
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb38;
- iVar5 = fclose(*(FILE **)(&stack0xffffefc4 + iVar1));
- if (iVar5 == 0) {
- return;
- }
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb55;
- piVar6 = __errno_location();
- iVar5 = *piVar6;
- *(undefined **)(&stack0xffffefc4 + iVar1) = &DAT_080ef360;
- *(int *)(BackupFilename + iVar1 + -0x20) = iVar5;
- *(undefined4 *)((int)&uStack_1040 + iVar1) = 0x804eb67;
- error(*(char **)(&stack0xffffefc4 + iVar1));
- return;
-}
-
void __static_initialization_and_destruction_0(int __initialize_p,int __priority)
{
diff --git a/src/script.cc b/src/script.cc
index 009fd86..5dd3751 100644
--- a/src/script.cc
+++ b/src/script.cc
@@ -117,6 +117,61 @@ static char *findLast(char *s, char c){
return Last;
}
+static void SaveFile(const char *Filename){
+ if(Filename == NULL){
+ error("SaveFile: Filename ist NULL.\n");
+ return;
+ }
+
+ char BackupFilename[4096];
+
+ strcpy(BackupFilename, Filename);
+ strcat(BackupFilename, "#");
+
+ FILE *Source = fopen(Filename, "rb");
+ if(Source == NULL){
+ error("SaveFile: Quelldatei %s existiert nicht.\n", Filename);
+ return;
+ }
+
+ FILE *Dest = fopen(BackupFilename, "wb");
+ if(Dest == NULL){
+ error("SaveFile: Kann Zieldatei %s nicht anlegen.\n", BackupFilename);
+ fclose(Source);
+ return;
+ }
+
+ // NOTE(fusion): This function was in pretty bad shape after this point. The
+ // reason is probably because GHIDRA didn't make sense of it using `alloca`
+ // or some other mechanism to allocate a buffer on the stack to copy the whole
+ // file in one go. It's probably not a good idea to mimic that here so we'll
+ // do it gradually.
+
+ char Buffer[KB(16)];
+ while(true){
+ usize n = fread(Buffer, 1, sizeof(Buffer), Source);
+ if(n == 0){
+ if(ferror(Source)){
+ error("SaveFile: Fehler beim Lesen der Quelldatei %s.\n", Filename);
+ }
+ break;
+ }
+
+ if(fwrite(Buffer, 1, n, Dest) != n){
+ error("SaveFile: Fehler beim Schreiben der Zieldatei %s.\n", BackupFilename);
+ break;
+ }
+ }
+
+ if(fclose(Source) != 0){
+ error("SaveFile: Fehler %d beim Schließen der Quelldatei.\n", errno);
+ }
+
+ if(fclose(Dest) != 0){
+ error("SaveFile: Fehler %d beim Schließen der Zieldatei.\n", errno);
+ }
+}
+
// TReadScriptFile
//==============================================================================
TReadScriptFile::TReadScriptFile(void){