From b3b4fb39ef000cf05ec456f9fe9198c0208bfa4a Mon Sep 17 00:00:00 2001 From: oz Date: Sun, 25 Oct 2020 17:17:26 +0300 Subject: [PATCH] Loader v1. --- SpaceCadetPinball/SpaceCadetPinball.cpp | 6 + SpaceCadetPinball/SpaceCadetPinball.vcxproj | 4 + .../SpaceCadetPinball.vcxproj.filters | 12 + SpaceCadetPinball/loader.cpp | 315 ++++++++++++++++++ SpaceCadetPinball/loader.h | 44 +++ SpaceCadetPinball/pch.h | 5 +- SpaceCadetPinball/pinball.cpp | 5 + SpaceCadetPinball/pinball.h | 8 + 8 files changed, 397 insertions(+), 2 deletions(-) create mode 100644 SpaceCadetPinball/loader.cpp create mode 100644 SpaceCadetPinball/loader.h create mode 100644 SpaceCadetPinball/pinball.cpp create mode 100644 SpaceCadetPinball/pinball.h diff --git a/SpaceCadetPinball/SpaceCadetPinball.cpp b/SpaceCadetPinball/SpaceCadetPinball.cpp index 2dcb374..1fb032b 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.cpp +++ b/SpaceCadetPinball/SpaceCadetPinball.cpp @@ -7,6 +7,7 @@ #include "objlist_class.h" #include "partman.h" #include "DatParser.h" +#include "loader.h" int main() { @@ -38,6 +39,11 @@ int main() assert(memcmp(partman::field_labeled(datFile, "table_size", ShortArray), new short[2]{ 600, 416 }, 2 * 2) == 0); + //loader::error(25, 26); + loader::loadfrom(datFile); + + loader::get_sound_id(18); + // //DatParser::Parse(dataFileName); } diff --git a/SpaceCadetPinball/SpaceCadetPinball.vcxproj b/SpaceCadetPinball/SpaceCadetPinball.vcxproj index 4d486b8..fcb8490 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.vcxproj +++ b/SpaceCadetPinball/SpaceCadetPinball.vcxproj @@ -153,12 +153,15 @@ + + + @@ -167,6 +170,7 @@ Create Create + diff --git a/SpaceCadetPinball/SpaceCadetPinball.vcxproj.filters b/SpaceCadetPinball/SpaceCadetPinball.vcxproj.filters index bf4cc9f..1cc0941 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.vcxproj.filters +++ b/SpaceCadetPinball/SpaceCadetPinball.vcxproj.filters @@ -27,6 +27,12 @@ Header Files + + Header Files + + + Header Files + @@ -44,6 +50,12 @@ Source Files + + Source Files + + + Source Files + diff --git a/SpaceCadetPinball/loader.cpp b/SpaceCadetPinball/loader.cpp new file mode 100644 index 0000000..6d0d9b2 --- /dev/null +++ b/SpaceCadetPinball/loader.cpp @@ -0,0 +1,315 @@ +#include "pch.h" +#include "loader.h" + +#include "partman.h" +#include "pinball.h" + + +/*_loader_errors dd 0, offset aBadHandle, 1, offset aNoTypeField, 2, offset aNoAttributesFi + + dd 0Bh, offset aNoFloatAttribu, 3, offset aWrongTypeMater; "Unknown" ... + dd 4, offset aWrongTypeKicke, 5, offset aWrongTypeAnObj, 6 + dd offset aWrongTypeAStat, 7, offset aStatesReDefine, 9, offset aUnrecognizedAt + dd 0Ah, offset aUnrecognizedFl, 0Dh, offset aFloatAttribute + dd 0Ch, offset aStateIndexOutO, 0Fh, offset aLoaderMaterial_0 + dd 0Eh, offset aLoaderKickerRe, 10h, offset aLoaderStateIdR + dd 8, offset aWallsDoesnTMat, 11h, offset aLoaderQueryVis_0 + dd 12h, offset aLoaderQueryVis, 15h, offset aLoaderMaterial + dd 14h, offset aLoaderKicker, 16h, offset aLoaderQueryAtt + dd 17h, offset aLoaderQueryIat, 13h, offset aLoaderQueryNam + dd 18h, offset aLoaderStateId, 19h, offset aLoaderGetSound + dd 1Ah, offset aSoundReference, 0FFFFFFFFh, offset aUnknown*/ + + +errorMsg loader::loader_errors[] = { + errorMsg{0, "Bad Handle"}, + errorMsg{1, "No Type Field"}, + errorMsg{2, "No Attributes Field"}, + errorMsg{0x0B, "No float Attributes Field"}, + errorMsg{3, "Wrong Type: MATERIAL Expected"}, + errorMsg{4, "Wrong Type: KICKER Expected"}, + errorMsg{5, "Wrong Type: AN_OBJECT Expected"}, + errorMsg{6, "Wrong Type: A_STATE Expected"}, + errorMsg{7, "STATES (re)defined in a state"}, + errorMsg{9, "Unrecognized Attribute"}, + errorMsg{0x0A, "Unrecognized float Attribute"}, + errorMsg{0x0D, "float Attribute not found"}, + errorMsg{0x0C, "state_index out of range"}, + errorMsg{0x0F, "loader_material() reports failure"}, + errorMsg{0x0E, "loader_kicker() reports failure"}, + errorMsg{0x10, "loader_state_id() reports failure"}, + errorMsg{0x8, "# walls doesn't match data size"}, + errorMsg{0x11, "loader_query_visual_states()"}, + errorMsg{0x12, "loader_query_visual()"}, + errorMsg{0x15, "loader_material()"}, + errorMsg{0x14, "loader_kicker()"}, + errorMsg{0x16, "loader_query_attribute()"}, + errorMsg{0x17, "loader_query_iattribute()"}, + errorMsg{0x13, "loader_query_name()"}, + errorMsg{0x18, "loader_state_id()"}, + errorMsg{0x19, "loader_get_sound_id()"}, + errorMsg{0x1A, "sound reference is not A_SOUND record"}, + errorMsg{-1, "Unknown"}, +}; + +int loader::sound_count = 1; +int loader::loader_sound_count; +datFileStruct* loader::loader_table; +datFileStruct* loader::sound_record_table; +soundListStruct loader::sound_list[65]; + +int loader::error(int errorCode, int captionCode) +{ + int curCode = loader_errors[0].Code; + const char *errorText = nullptr, *errorCaption = nullptr; + int index = 0, index2 = 0; + if (loader_errors[0].Code >= 0) + do + { + if (errorCode == curCode) + errorText = loader_errors[index2].Message; + if (captionCode == curCode) + errorCaption = loader_errors[index2].Message; + index2 = ++index; + curCode = loader_errors[index].Code; + } + while (curCode >= 0); + if (!errorText) + errorText = loader_errors[index].Message; + MessageBoxA(nullptr, errorText, errorCaption, 0x2000u); + return -1; +} + +void loader::default_vsi(int* arr) +{ + arr[13] = 0; + arr[5] = 1369940824; + arr[12] = 0; + arr[0] = 1064514355; + arr[1] = 1058642330; + arr[2] = 0; + arr[4] = 0; + arr[16] = 0; + arr[17] = 0; + arr[15] = 0; + arr[14] = 0; +} + +void loader::loadfrom(datFileStruct* datFile) +{ + int groupIndex = 0; + loader_table = datFile; + sound_record_table = loader_table; + int soundIndex = sound_count; + if (datFile->NumberOfGroups > 0) + { + do + { + __int16* value = (__int16*)partman::field(datFile, groupIndex, ShortValue); + if (value && *value == 202) + { + soundIndex = sound_count; + if (sound_count < 65) + { + sound_list[soundIndex].WavePtr = nullptr; + sound_list[soundIndex].GroupIndex = groupIndex; + sound_count = ++soundIndex; + } + } + ++groupIndex; + } + while (groupIndex < datFile->NumberOfGroups); + } + loader_sound_count = soundIndex; +} + +void loader::unload() +{ + int index = 1; + if (sound_count > 1) + { + soundListStruct* soundListPtr = &sound_list[1]; + do + { + //Sound_FreeSound(soundListPtr->WavePtr); + ++index; + ++soundListPtr; + } + while (index < sound_count); + } + if (sound_list[index].PtrToSmth) + memoryfree(sound_list[index].PtrToSmth); + sound_count = 1; +} + +int loader::get_sound_id(int groupIndex) +{ + int result; + __int16 soundIndex = 1; + if (sound_count <= 1) + { + error(25, 26); + result = -1; + } + else + { + while (sound_list[soundIndex].GroupIndex != groupIndex) + { + ++soundIndex; + if (soundIndex >= sound_count) + { + error(25, 26); + return -1; + } + } + if (!sound_list[soundIndex].Loaded && !sound_list[soundIndex].WavePtr) + { + int soundGroupId = sound_list[soundIndex].GroupIndex; + sound_list[soundIndex].Volume = 0.0; + if (soundGroupId > 0 && !pinball::quickFlag) + { + __int16* value = (__int16*)partman::field(loader_table, soundGroupId, ShortValue); + if (value && *value == 202) + { + const CHAR* fileName = partman::field(loader_table, soundGroupId, String); + HFILE hFile = _lopen(fileName, 0); + sound_list[soundIndex].Volume = static_cast(_llseek(hFile, 0, 2)) * 0.0000909090909090909; + _lclose(hFile); + //sound_list[soundIndex4].WavePtr = Sound_LoadWaveFile(lpName); + } + } + } + ++sound_list[soundIndex].Loaded; + result = soundIndex; + } + return result; +} + + +int loader::query_handle(LPCSTR lpString) +{ + return partman::record_labeled(loader_table, lpString); +} + +short loader::query_visual_states(int groupIndex) +{ + short result; + if (groupIndex < 0) + return error(0, 17); + __int16* shortArr = (__int16*)partman::field(loader_table, groupIndex, ShortArray); + if (shortArr && *shortArr == 100) + result = shortArr[1]; + else + result = 1; + return result; +} + +char* loader::query_name(int groupIndex) +{ + if (groupIndex >= 0) + return partman::field(loader_table, groupIndex, GroupName); + error(0, 19); + return nullptr; +} + +__int16* loader::query_iattribute(int groupIndex, int firstValue, int* arraySize) +{ + __int16* result; + __int16 skipIndex = 0; + if (groupIndex >= 0) + { + while (true) + { + __int16* shortArr = (__int16*)partman::field_nth(loader_table, groupIndex, ShortArray, skipIndex); + if (!shortArr) + break; + if (*shortArr == firstValue) + { + *arraySize = partman::field_size(loader_table, groupIndex, ShortArray) / 2 - 1; + return shortArr + 1; + } + ++skipIndex; + } + error(2, 23); + *arraySize = 0; + result = nullptr; + } + else + { + error(0, 22); + result = nullptr; + } + return result; +} + +float* loader::query_float_attribute(int groupIndex, int groupIndexOffset, int firstValue) +{ + float* result; + __int16 skipIndex = 0; + if (groupIndex >= 0) + { + int groupIndexSum = state_id(groupIndex, groupIndexOffset); + if (groupIndexSum >= 0) + { + while (true) + { + float* floatArr = (float*)partman::field_nth(loader_table, groupIndexSum, FloatArray, skipIndex); + if (!floatArr) + break; + if (static_cast<__int16>(static_cast<__int64>(floor(*floatArr))) == firstValue) + return floatArr + 1; + ++skipIndex; + } + error(13, 22); + result = nullptr; + } + else + { + error(16, 22); + result = nullptr; + } + } + else + { + error(0, 22); + result = nullptr; + } + return result; +} + + +double loader::play_sound(int soundIndex) +{ + if (soundIndex <= 0) + return 0.0; + //Sound_PlaySound(sound_list[soundIndex].WavePtr, 0, 7, 5, 0); + return sound_list[soundIndex].Volume; +} + +int loader::state_id(int groupIndex, int groupIndexOffset) +{ + int result; + int groupIndex2 = groupIndex; + __int16 visualState = query_visual_states(groupIndex); + if (visualState <= 0) + return error(12, 24); + __int16* shortArr = (__int16*)partman::field(loader_table, groupIndex, ShortValue); + if (!shortArr) + return error(1, 24); + if (*shortArr != 200) + return error(5, 24); + if (groupIndexOffset > visualState) + return error(12, 24); + if (!groupIndexOffset) + return groupIndex2; + + groupIndex2 = groupIndexOffset + groupIndex; + shortArr = (__int16*)partman::field(loader_table, groupIndexOffset + groupIndex, ShortValue); + if (!shortArr) + return error(1, 24); + if (*shortArr != 201) + result = error(6, 24); + else + result = groupIndex2; + return result; +} diff --git a/SpaceCadetPinball/loader.h b/SpaceCadetPinball/loader.h new file mode 100644 index 0000000..58a54bf --- /dev/null +++ b/SpaceCadetPinball/loader.h @@ -0,0 +1,44 @@ +#pragma once + + +struct datFileStruct; + +struct errorMsg +{ + int Code; + const char* Message; +}; + +struct soundListStruct +{ + char* WavePtr; + int GroupIndex; + int Loaded; + float Volume; + char* PtrToSmth; +}; + + +class loader +{ +public: + static int error(int errorCode, int captionCode); + static void default_vsi(int* arr); + static void loadfrom(datFileStruct* datFile); + static void unload(); + static int get_sound_id(int groupIndex); + static int query_handle(LPCSTR lpString); + static short query_visual_states(int groupIndex); + static char* query_name(int groupIndex); + static double play_sound(int soundIndex); + static __int16* query_iattribute(int groupIndex, int firstValue, int* arraySize); + static float* query_float_attribute(int groupIndex, int groupIndexOffset, int firstValue); + static int state_id(int groupIndex, int groupIndexOffset); +private: + static errorMsg loader_errors[]; + static datFileStruct* loader_table; + static datFileStruct* sound_record_table; + static int sound_count; + static int loader_sound_count; + static soundListStruct sound_list[65]; +}; diff --git a/SpaceCadetPinball/pch.h b/SpaceCadetPinball/pch.h index 40ca0eb..ee65bcc 100644 --- a/SpaceCadetPinball/pch.h +++ b/SpaceCadetPinball/pch.h @@ -10,9 +10,10 @@ #define PCH_H // TODO: add headers that you want to pre-compile here -#include +#include #include -#include +#include +#include #define memoryallocate(x) malloc(x); #define memoryfree(x) free(x); diff --git a/SpaceCadetPinball/pinball.cpp b/SpaceCadetPinball/pinball.cpp new file mode 100644 index 0000000..5b10c53 --- /dev/null +++ b/SpaceCadetPinball/pinball.cpp @@ -0,0 +1,5 @@ +#include "pch.h" +#include "pinball.h" + + +int pinball::quickFlag = 0; \ No newline at end of file diff --git a/SpaceCadetPinball/pinball.h b/SpaceCadetPinball/pinball.h new file mode 100644 index 0000000..38f56fd --- /dev/null +++ b/SpaceCadetPinball/pinball.h @@ -0,0 +1,8 @@ +#pragma once +class pinball +{ +public: + static int quickFlag; +private: +}; +