summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjohannes <johannes.herman@gmail.com>2024-02-09 20:09:42 +0100
committerjohannes <johannes.herman@gmail.com>2024-02-09 20:09:42 +0100
commit5fa16e90770a73bba17efa23bf8b24a0d48e3a81 (patch)
tree519c319762ba955e0a6788696b33dcbcbecd04a2 /src
parent26b74cfde46e3ed0be4759946ab5ab43cc5d669b (diff)
added change name and skin functions
Diffstat (limited to 'src')
-rw-r--r--src/commands.c102
-rw-r--r--src/demo.c4
-rw-r--r--src/main.c14
-rw-r--r--src/pack.c1
4 files changed, 120 insertions, 1 deletions
diff --git a/src/commands.c b/src/commands.c
new file mode 100644
index 0000000..f83909e
--- /dev/null
+++ b/src/commands.c
@@ -0,0 +1,102 @@
+#include "../inc/demo.h"
+#include "../inc/pack.h"
+
+#include <string.h>
+
+// TODO set by id and name has much repeating, fix that
+int setnamebyid(int id, char *new, demo *demo)
+{
+ int newlen = strlen(new);
+ if (newlen >= 15)
+ return -1;
+
+ int intname[4] = {0x80808080, 0x80808080, 0x80808080, 0x80808000};
+ strtoint(new, newlen + 1, intname);
+ int found = 0;
+
+ for (int i = 0; i < demo->data.numchunks; i++)
+ {
+ if (demo->data.chunks[i].type != DEMOSNAP)
+ continue;
+ demosnap *snap = demo->data.chunks[i].data.snap;
+
+ for (int y = 0; y < snap->numitems; y++)
+ {
+ demosnapitem *item = &snap->items[y];
+ if (item->type == 11 && item->id == id)
+ {
+ memcpy(item->data, intname, 4 * sizeof(int));
+ found++;
+ }
+ }
+ }
+ return found;
+}
+
+int setnamebyname(char *old, char *new, demo *demo)
+{
+ int newlen = strlen(new);
+ if (newlen >= 15)
+ return -1;
+
+ int intname[4] = {0x80808080, 0x80808080, 0x80808080, 0x80808000};
+ strtoint(new, newlen + 1, intname);
+ char found = 0;
+
+ for (int i = 0; i < demo->data.numchunks; i++)
+ {
+ if (demo->data.chunks[i].type != DEMOSNAP)
+ continue;
+ demosnap *snap = demo->data.chunks[i].data.snap;
+
+ for (int y = 0; y < snap->numitems; y++)
+ {
+ demosnapitem *item = &snap->items[y];
+ if (item->type != 11)
+ continue;
+
+ char name[16];
+ intstostr(item->data, 4, name);
+ if (strcmp(old, name) == 0)
+ {
+ memcpy(item->data, intname, 4 * sizeof(int));
+ found++;
+ }
+ }
+ }
+ return found;
+}
+
+int setskinbyname(char *name, char *skin, demo *demo)
+{
+ int skinlen = strlen(skin);
+ if (skinlen >= 23)
+ return -1;
+
+ int intskin[5] = {0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808000};
+ strtoint(skin, skinlen + 1, intskin);
+ char found = 0;
+
+ for (int i = 0; i < demo->data.numchunks; i++)
+ {
+ if (demo->data.chunks[i].type != DEMOSNAP)
+ continue;
+ demosnap *snap = demo->data.chunks[i].data.snap;
+
+ for (int y = 0; y < snap->numitems; y++)
+ {
+ demosnapitem *item = &snap->items[y];
+ if (item->type != 11)
+ continue;
+
+ char iname[16];
+ intstostr(item->data, 4, iname);
+ if (strcmp(name, iname) == 0)
+ {
+ memcpy(&item->data[8], intskin, 5 * sizeof(int));
+ found++;
+ }
+ }
+ }
+ return found;
+}
diff --git a/src/demo.c b/src/demo.c
index d73b1a7..f5c346e 100644
--- a/src/demo.c
+++ b/src/demo.c
@@ -18,6 +18,8 @@ const char headermagic[7] = "TWDEMO\0";
const unsigned char mapmagic[] = {0x6b, 0xe6, 0xda, 0x4a, 0xce, 0xbd, 0x38, 0x0c,
0x9b, 0x5b, 0x12, 0x89, 0xc8, 0x42, 0xd7, 0x80};
+/* readers */
+
int readdemoheader(FILE *fp, demoheader *dh)
{
char magicbuff[sizeof(headermagic)];
@@ -300,6 +302,8 @@ int readdemo(FILE *fp, demo *demo)
return 1;
}
+/* writers */
+
int writedemoheader(FILE *fp, demoheader *dh)
{
unsigned char intbuff[4];
diff --git a/src/main.c b/src/main.c
index e3f0fcf..9c31a57 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,6 @@
#include "../inc/demo.h"
#include "../inc/huffman.h"
+#include "../inc/commands.h"
#include <stdio.h>
#include <stdlib.h>
@@ -119,7 +120,18 @@ int main()
demo d;
readdemo(fp, &d);
- printdemo(&d, 0);
+
+ // printdemo(&d, 1);
+
+ int retid = setnamebyid(1, "NamedByID", &d);
+ int retname = setnamebyname("New Hero", "NamedByName", &d);
+
+ //TODO quirk, when doing further edits keep previous ones in mind
+ int retskin = setskinbyname("NamedByName", "bomb", &d);
+
+ printf("id: %d name: %d\n", retid, retname);
+ printf("skin: %d\n", retskin);
+
writedemo(op, &d);
fclose(fp);
diff --git a/src/pack.c b/src/pack.c
index 9e1d4fe..c446f56 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -119,6 +119,7 @@ void intstostr(const int *ints, int num, char *str)
ints++;
num--;
}
+ str[-1] = 0;
}
void strtoint(const char *str, int num, int *ints)