summaryrefslogtreecommitdiff
path: root/src/T2DLL/GlobalFunc.cpp
blob: 71139b09498aeef0f801f8879c133cc5f4975bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#define ATOI_KLUDGE
#include "CResFile.h"
#include "GlobalFunc.h"
#include "T2BitImage.h"
#include "T2DLL.h"
#include "T2OptionPluginList.h"
#include "T2TowerDoc.h"
#include "T2WorldDef.h"

char *strtokEx(char * inStr, const char * inSep, char ** ioLast) {
#line 11
    _ASSERT(!inStr || !*ioLast);

    if (inStr) {
        *ioLast = NULL;
        inStr += _mbsspn((const unsigned char *) inStr, (const unsigned char *) inSep);
    }

    if (*ioLast)
        inStr = *ioLast;

    char *work = inStr;

    if (inStr) {
        work = inStr;
        inStr = (char *) _mbspbrk((const unsigned char *) inStr, (const unsigned char *) inSep);
        if (inStr) {
            *inStr = 0;
            inStr++;
            inStr += _mbsspn((const unsigned char *) inStr, (const unsigned char *) inSep);
            if (*inStr == 0)
                inStr = NULL;
        }

        *ioLast = inStr;
    }

    return work;
}

void InsetRect(RECT * ioRect, int inX, int inY) {
    ioRect->top += inY;
    ioRect->bottom -= inY;
    ioRect->left += inX;
    ioRect->right -= inX;
}

void SetPt(POINT * outPt, int inX, int inY) {
    outPt->x = inX;
    outPt->y = inY;
}

HBITMAP Create256DIBitmap(HDC inDC, int inWidth, int inHeight) {
    HBITMAP theBitmap = NULL;

    if (GetDeviceCaps(inDC, BITSPIXEL) == 8) {
        theBitmap = CreateBitmap(inWidth, inHeight, 1, 8, NULL);
    } else {
        Bitmap256 *bitmap = (Bitmap256 *) malloc(sizeof(Bitmap256));
        memset(&bitmap->header, 0, sizeof(BITMAPINFOHEADER));

        bitmap->header.biSize = sizeof(BITMAPINFOHEADER);
        bitmap->header.biWidth = inWidth;
        bitmap->header.biHeight = inHeight;
        bitmap->header.biBitCount = 8;
        bitmap->header.biPlanes = 1;
        bitmap->header.biClrUsed = 256;
        bitmap->header.biClrImportant = 256;

        for (int i = 0; i < 256; i++)
            bitmap->palette[i] = i;

        void *bits;
        theBitmap = CreateDIBSection(inDC, (const BITMAPINFO *) bitmap, DIB_PAL_COLORS, &bits, NULL, 0);

        free(bitmap);
    }

    return theBitmap;
}

LOGPALETTE *ConvACTPalette(ACTPALETTEENTRY * inPalette) {
    LOGPALETTE *pal = (LOGPALETTE *) malloc(sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));

    for (int i = 0; i < 256; i++) {
        pal->palPalEntry[i].peRed = inPalette[i].red;
        pal->palPalEntry[i].peGreen = inPalette[i].green;
        pal->palPalEntry[i].peBlue = inPalette[i].blue;
        pal->palPalEntry[i].peFlags = 0;
    }

    pal->palVersion = 0x300;
    pal->palNumEntries = 256;
    return pal;
}

LOGPALETTE *LoadACTPaletteEntries(HINSTANCE inModule, int inResID) {
    HRSRC theRes = FindResource(inModule, MAKEINTRESOURCE(inResID), "PALETTE");
    if (!theRes)
        return NULL;

    HGLOBAL theResHandle = LoadResource(inModule, theRes);
    if (SizeofResource(inModule, theRes) == 0x300) {
        ACTPALETTEENTRY *act = (ACTPALETTEENTRY *) LockResource(theResHandle);
        return ConvACTPalette(act);
    }

    unsigned char *data = (unsigned char *) LockResource(theResHandle);
    DWORD theResSize = sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY);
    LOGPALETTE *theLogPalette = (LOGPALETTE *) malloc(theResSize);
    memset(theLogPalette, 0, theResSize);

    theLogPalette->palVersion = 0x300;
    theLogPalette->palNumEntries = 256;

    if (data[0] == 0 && data[1] == 0) {
        data += 6;

        int entryCount = (data[0] << 8) + data[1];
        data += 2;

        for (int i = 0; i < entryCount; i++) {
            theLogPalette->palPalEntry[data[1]].peRed = data[2];
            theLogPalette->palPalEntry[data[1]].peGreen = data[4];
            theLogPalette->palPalEntry[data[1]].peBlue = data[6];
            theLogPalette->palPalEntry[data[1]].peFlags = 0;
            data += 8;
        }
    } else {
        int entryCount = (data[0] << 8) + data[1];
        data += 16;

        for (int i = 0; i < entryCount; i++) {
            theLogPalette->palPalEntry[i].peRed = data[0];
            theLogPalette->palPalEntry[i].peGreen = data[2];
            theLogPalette->palPalEntry[i].peBlue = data[4];
            theLogPalette->palPalEntry[i].peFlags = 0;
            data += 16;
        }
    }

    return theLogPalette;
}

// 1001719C, not exported lmao
CPalette *LoadPaletteUnkName(HINSTANCE inModule, int inResID) {
    LOGPALETTE *logPalette = LoadACTPaletteEntries(inModule, inResID);
    if (!logPalette)
        return NULL;

    logPalette->palPalEntry[253].peFlags = PC_RESERVED;
    logPalette->palPalEntry[254].peFlags = PC_RESERVED;

    CPalette *thePalette = new CPalette;
    thePalette->CreatePalette(logPalette);
    free(logPalette);

    return thePalette;
}

void FFSetStaticForce(int inX, int inY) {
    if (!FFEnable)
        return;

    LONG rglDirection[2] = { inX, inY };
    DICONSTANTFORCE cf;
    cf.lMagnitude = (DWORD) sqrt((double)inX * (double)inX + (double)inY * (double)inY);

    DIEFFECT eff;
    eff.dwSize = sizeof(DIEFFECT);
    eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
    eff.cAxes = 2;
    eff.rglDirection = rglDirection;
    eff.lpEnvelope = NULL;
    eff.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
    eff.lpvTypeSpecificParams = &cf;

    FFStatic->SetParameters(&eff, DIEP_DIRECTION | DIEP_TYPESPECIFICPARAMS | DIEP_START);
}

CString GetInstallSource() {
    return GetMachineProfileString("Install", "Source", "Z:\\");
}

CString GetInstallSourceDrive() {
    CString path = GetMachineProfileString("Install", "Source", "Z:\\");
    return path.Left(1);
}

T2TowerDoc *GetCurrentT2TowerDoc() {
    return g_TowerDoc;
}

HINSTANCE GetWorldModuleHandle() {
    return GetCurrentT2TowerDoc()->GetWorldDef()->mModuleHandle;
}

CString GetModuleName(HINSTANCE inModule) {
    char buf[512];
    GetModuleFileName(inModule, buf, 512);

    CString theName = _mbsrchr((const unsigned char *) buf, '\\') + 1;
    return theName;
}

unsigned int TickCount() {
    return (GetTickCount() * 60) / 1000;
}

BOOL DispatchOptionEvent(OPTIONEVENTTYPE inEventType, void * inData) {
    return GetCurrentT2TowerDoc()->mOptionPluginList->DispatchEvent(inEventType, GetCurrentT2TowerDoc(), inData);
}

CFile *OpenSubPluginFS(const char * inPath, int inID1, int inID2, SUBPLUGINFSENTRY * outEntry) {
    CFile *file = new CFile;

    if (!file->Open(inPath, CFile::modeRead)) {
        delete file;
        return NULL;
    }

    char magic[2];
    file->Read(magic, 2);
#line 229
    _ASSERT(memcmp(magic, "SP", 2) == 0);

    unsigned int curEntry, count;
    SUBPLUGINFSENTRY fsEntry;
    file->Read(&count, sizeof(count));

    for (curEntry = 0; curEntry < count; curEntry++) {
        file->Read(&fsEntry, sizeof(fsEntry));
        if (fsEntry.spfse_m0 == inID2 && fsEntry.spfse_m4 == inID1)
            break;
    }

    if (curEntry == count) {
        file->Close();
        delete file;

        CString err;
        err.Format("CResFile::OpenResource(SubPlugin FS) ERROR : %s,%d,%d\n", inPath, inID2, inID1);
        OutputDebugString(err);
        return NULL;
    }

    *outEntry = fsEntry;
    file->Seek(fsEntry.spfse_m8, CFile::begin);
    return file;
}

void __Rep0(const char * inFileName, int inLineNum, const char * inMessage) {
    CString err;
    err.Format("%s (%d)\n\n%s", inFileName, inLineNum, inMessage);
    MessageBox(NULL, err, "ERROR", MB_ICONERROR);
    _CrtDbgBreak();
}

void __Rep1(const char * inFileName, int inLineNum, const char * inMessage, unsigned long inArg1) {
    CString err;
    err.Format(inMessage, inArg1);
    __Rep0(inFileName, inLineNum, err);
}

void __Rep2(const char * inFileName, int inLineNum, const char * inMessage, unsigned long inArg1, unsigned long inArg2) {
    CString err;
    err.Format(inMessage, inArg1, inArg2);
    __Rep0(inFileName, inLineNum, err);
}

void GetTowerDirectory(char * outStr) {
    GetModuleFileName(NULL, outStr, 1000);
    *strrchr(outStr, '\\') = 0;

    char *p = strrchr(outStr, '\\');
    if (p && !_stricmp(p, "\\debug"))
        *p = 0;
    else if (p && !_stricmp(p, "\\release"))
        *p = 0;

    strcat(outStr, "\\");
}

CString GetTowerDirectory() {
    char *buffer = (char *) malloc(1000);
    GetTowerDirectory(buffer);
    CString thePath = buffer;
    free(buffer);
    return thePath;
}

CString LoadString(HINSTANCE inModule, int inID, int inMaxSize) {
    if (inModule == INSTANCE_EXE)
        inModule = AfxGetInstanceHandle();
    else if (inModule == INSTANCE_WORLD)
        inModule = GetWorldModuleHandle();

    CString str;
    char *buffer = (char *) malloc(inMaxSize + 1);
    buffer[0] = 0;

    if (inModule != INSTANCE_ANY) {
        str.Format("%d@%s", inID, GetModuleName(inModule));
        LoadString(inModule, inID, buffer, inMaxSize + 1);
    } else {
        str.Format("%d", inID);
        if (!LoadString(GetWorldModuleHandle(), inID, buffer, inMaxSize + 1))
            LoadString(AfxGetInstanceHandle(), inID, buffer, inMaxSize + 1);
    }

    if (buffer[0] != 0)
        str = buffer;

    free(buffer);
    return str;
}

CString LoadStringTable(HINSTANCE inModule, int inID1, int inID2) {
    if (inModule == INSTANCE_EXE)
        inModule = AfxGetInstanceHandle();
    else if (inModule == INSTANCE_WORLD)
        inModule = GetWorldModuleHandle();

    CString str;

    if (inModule != INSTANCE_ANY) {
        str.Format("%d-%d@%s", inID1, inID2, GetModuleName(inModule));
        CResFile theResFile;
        if (theResFile.OpenResource(inModule, inID1, "STRTABLE")) {
            int count;
            theResFile >> count;
            if (inID2 <= count) {
                for (int i = 1; i <= inID2; i++)
                    theResFile >> str;
            }
        }
    } else {
        str.Format("%d-%d", inID1, inID2);

        BOOL isOpen = false;
        CResFile theResFile;
        if (theResFile.OpenResource(GetWorldModuleHandle(), inID1, "STRTABLE"))
            isOpen = true;
        else if (theResFile.OpenResource(AfxGetInstanceHandle(), inID1, "STRTABLE"))
            isOpen = true;

        if (isOpen) {
            int count;
            theResFile >> count;
            if (inID2 <= count) {
                for (int i = 1; i <= inID2; i++)
                    theResFile >> str;
            }
        }
    }

    return str;
}

void CopyPalette(LOGPALETTE *inSrc, LOGPALETTE *inDst, int inSrcIndex, int inDstIndex, int inCount) {
    for (int i = 0; i < inCount; i++) {
        inDst->palPalEntry[inDstIndex + i].peRed = inSrc->palPalEntry[inSrcIndex + i].peRed;
        inDst->palPalEntry[inDstIndex + i].peGreen = inSrc->palPalEntry[inSrcIndex + i].peGreen;
        inDst->palPalEntry[inDstIndex + i].peBlue = inSrc->palPalEntry[inSrcIndex + i].peBlue;
    }
}

void WriteMachineProfileString(const char * inKey, const char * inName, const char * inValue) {
    CString subKey = "SOFTWARE\\OPeNBooK9003\\T2\\";
    subKey += inKey;

    HKEY key;
    RegCreateKey(HKEY_LOCAL_MACHINE, subKey, &key);
    RegSetValueEx(key, inName, 0, REG_SZ, (LPBYTE) inValue, strlen(inValue));
    RegCloseKey(key);
}

void WriteMachineProfileInt(const char * inKey, const char * inName, int inValue) {
    CString value;
    value.Format("%d", inValue);
    WriteMachineProfileString(inKey, inName, value);
}

CString GetMachineProfileString(const char * inKey, const char * inName, const char * inDefaultValue) {
    CString subKey = "SOFTWARE\\OPeNBooK9003\\T2\\";
    subKey += inKey;

    CString result;

    HKEY key;
    if (RegOpenKey(HKEY_LOCAL_MACHINE, subKey, &key) == ERROR_SUCCESS) {
        DWORD type = REG_SZ;
        DWORD bufferSize = 0x400;
        char *buffer = (char *) malloc(bufferSize);
        if (RegQueryValueEx(key, inName, NULL, &type, (LPBYTE) buffer, &bufferSize) == ERROR_SUCCESS)
            result = buffer;
        else
            result = inDefaultValue;

        RegCloseKey(key);
        free(buffer);
    } else {
        result = inDefaultValue;
    }

    return result;
}

int GetMachineProfileInt(const char * inKey, const char * inName, int inDefaultValue) {
    CString value;
    value.Format("%d", inDefaultValue);
    value = GetMachineProfileString(inKey, inName, value);
    return atoi(value);
}