Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a different approach to check entity for modstorage #115

Merged
merged 21 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2b4f212
Use a different approach to check entity for modstorage
lava76 Apr 4, 2022
87d8598
Need to reset id to empty string
lava76 Apr 4, 2022
70ba9cc
ffs
lava76 Apr 4, 2022
63f5d9a
Fixed wrong type
lava76 Apr 5, 2022
9c4e571
Worked around issue of NULL identity in OnStoreLoad
lava76 Apr 6, 2022
05c0354
Use queued ID in AddEntity as well
lava76 Apr 7, 2022
bfb2779
Make sure player doesn't get a stale queued ID and prefer identity ID
lava76 Apr 7, 2022
b4211d7
v5, fixed memory leak leading to CTD and storage corruption, optimiza…
lava76 Apr 7, 2022
3f0023a
Ignore ModStorage module on client (main menu)
lava76 Apr 8, 2022
b6dce8c
Fixed wrong data types being read for vector components
lava76 Apr 9, 2022
05733f1
Disable ModStorage on client completely
lava76 Apr 9, 2022
d409960
Revert "Disable ModStorage on client completely"
lava76 Apr 9, 2022
e6485fe
Bail on old ModStorage data to avoid possible CTD if corrupted storage
lava76 Apr 22, 2022
dbe8c1b
Merge branch 'development' of https://github.com/Jacob-Mango/DayZ-Com…
lava76 Nov 29, 2023
3d9ffa2
Defines moved to config.cpp
lava76 Jul 12, 2024
778768a
Sync with origin
lava76 Jul 12, 2024
e100a03
Merge branch 'Arkensor:development' into development
lava76 Jul 12, 2024
292236a
Removed MS disable defines
lava76 Aug 22, 2024
4b335b3
Added BoatScript CF_ModStorage support
lava76 Sep 27, 2024
1b4adbd
Deal with BoatScript CF data not being present
lava76 Jan 4, 2025
71f1907
Bumped storage version to 141 (DayZ 1.27)
lava76 Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 179 additions & 27 deletions JM/CF/Scripts/3_Game/CommunityFramework/ModStorage/CF_ModStorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@
*/
class CF_ModStorage
{
static const int VERSION = 4;
static const int VERSION = 5;

static const int GAME_VERSION_FIRST_INSTALL = 116;
static const int GAME_VERSION_WIPE_FILE = 127;
static const int GAME_VERSION_WIPE_FILE = 141; //! DayZ 1.27 GAME_STORAGE_VERSION

static const int MODSTORAGE_INITIAL_IMPLEMENTATION = 2;

int m_CF_Version;

ModStructure m_Mod;

int m_Version;

int m_HashA;
int m_HashB;

// v4
string m_Data;
string m_Value;

// v5
ref array<ref CF_ModStorageDataBase> m_Entries;
int m_Idx;
int m_MaxIdx;

void CF_ModStorage(ModStructure mod)
{
m_Mod = mod;
Expand All @@ -40,91 +48,151 @@ class CF_ModStorage
return m_Mod;
}

string GetModName()
{
if (m_Mod)
{
return m_Mod.GetName();
}

return "unknown<" + m_HashA + "," + m_HashB + ">";
}

bool Read(out bool value)
{
m_Data.ParseStringEx(m_Value);
if (m_CF_Version < VERSION)
{
m_Data.ParseStringEx(m_Value);

value = m_Value == "1";
value = m_Value == "1";
}
else
{
if (m_Idx > m_MaxIdx || m_Entries[m_Idx].ValueType() != bool)
return false;

value = CF_ModStorageData<bool>.Cast(m_Entries[m_Idx++]).Get();
}

return true;
}

bool Read(out int value)
{
m_Data.ParseStringEx(m_Value);
if (m_CF_Version < VERSION)
{
m_Data.ParseStringEx(m_Value);

value = m_Value.ToInt();
value = m_Value.ToInt();
}
else
{
if (m_Idx > m_MaxIdx || m_Entries[m_Idx].ValueType() != int)
return false;

value = CF_ModStorageData<int>.Cast(m_Entries[m_Idx++]).Get();
}

return true;
}

bool Read(out float value)
{
m_Data.ParseStringEx(m_Value);
if (m_CF_Version < VERSION)
{
m_Data.ParseStringEx(m_Value);

value = m_Value.ToFloat();
value = m_Value.ToFloat();
}
else
{
if (m_Idx > m_MaxIdx || m_Entries[m_Idx].ValueType() != float)
return false;

value = CF_ModStorageData<float>.Cast(m_Entries[m_Idx++]).Get();
}

return true;
}

bool Read(out vector value)
{
m_Data.ParseStringEx(m_Value);
if (m_CF_Version < VERSION)
{
m_Data.ParseStringEx(m_Value);

value = m_Value.ToVector();
value = m_Value.ToVector();
}
else
{
if (m_Idx > m_MaxIdx || m_Entries[m_Idx].ValueType() != vector)
return false;

value = CF_ModStorageData<vector>.Cast(m_Entries[m_Idx++]).Get();
}

return true;
}

bool Read(out string value)
{
m_Data.ParseStringEx(m_Value);
if (m_CF_Version < VERSION)
{
m_Data.ParseStringEx(m_Value);

value = m_Value;
value = m_Value;
}
else
{
if (m_Idx > m_MaxIdx || m_Entries[m_Idx].ValueType() != string)
return false;

value = CF_ModStorageData<string>.Cast(m_Entries[m_Idx++]).Get();
}

return true;
}

void Write(bool value)
{
if (value)
{
m_Data += " 1";
}
else
{
m_Data += " 0";
}
_Insert(value);
}

void Write(int value)
{
m_Data += " " + value.ToString();
_Insert(value);
}

void Write(float value)
{
m_Data += " " + value.ToString();
_Insert(value);
}

void Write(vector value)
{
m_Data += " \"" + value.ToString(false) + "\"";
_Insert(value);
}

void Write(string value)
{
m_Data += " \"" + value + "\"";
_Insert(value);
}

void _CopyStreamTo(Serializer ctx)
{
int cf_version = m_CF_Version;
string data;
if (m_Data.Length() > 0)

if (cf_version < VERSION)
{
data = m_Data.Substring(1, m_Data.Length() - 1);
if (m_Data.Length() > 0)
{
data = m_Data.Substring(1, m_Data.Length() - 1);
}
}

auto tmp = m_Entries;

// force resetting early so we can write the latest version
_ResetStream();

Expand All @@ -133,22 +201,106 @@ class CF_ModStorage

ctx.Write(m_Version);

ctx.Write(data);
if (cf_version < VERSION)
{
ctx.Write(data);
return;
}

ctx.Write(tmp.Count());
foreach (auto entry: tmp)
{
switch (entry.ValueType())
{
case bool:
CF_ModStorageData<bool> entryBool = CF_ModStorageData<bool>.Cast(entry);
ctx.Write(CF_ModStorageDataType.BOOL);
ctx.Write(entryBool.Get());
break;
case int:
CF_ModStorageData<int> entryInt = CF_ModStorageData<int>.Cast(entry);
ctx.Write(CF_ModStorageDataType.INT);
ctx.Write(entryInt.Get());
break;
case float:
CF_ModStorageData<float> entryFloat = CF_ModStorageData<float>.Cast(entry);
ctx.Write(CF_ModStorageDataType.FLOAT);
ctx.Write(entryFloat.Get());
break;
case vector:
CF_ModStorageData<vector> entryVector = CF_ModStorageData<vector>.Cast(entry);
ctx.Write(CF_ModStorageDataType.VECTOR);
vector v = entryVector.Get();
ctx.Write(v[0]);
ctx.Write(v[1]);
ctx.Write(v[2]);
break;
case string:
CF_ModStorageData<string> entryString = CF_ModStorageData<string>.Cast(entry);
ctx.Write(CF_ModStorageDataType.STRING);
ctx.Write(entryString.Get());
break;
default:
CF_Log.Error("Failed to write unknown data type %1 for mod %2", entry.Type().ToString(), GetModName());
}
}
}

// Read and Write functions can't be called, so we can't reset the stream
void _ResetStream()
{
m_Idx = 0;

if (!m_Mod)
{
if (!m_Entries)
{
m_Entries = new array<ref CF_ModStorageDataBase>();
m_MaxIdx = -1;
}
return;
}

m_CF_Version = VERSION;

m_Entries = new array<ref CF_ModStorageDataBase>();
m_MaxIdx = -1;

m_Data = string.Empty;

m_HashA = m_Mod.m_CF_HashA;
m_HashB = m_Mod.m_CF_HashB;

m_Version = m_Mod.GetStorageVersion();
}

void _Insert(bool b)
{
m_Entries.Insert(new CF_ModStorageData<bool>(b));
m_MaxIdx++;
}

void _Insert(int i)
{
m_Entries.Insert(new CF_ModStorageData<int>(i));
m_MaxIdx++;
}

void _Insert(float f)
{
m_Entries.Insert(new CF_ModStorageData<float>(f));
m_MaxIdx++;
}

void _Insert(vector v)
{
m_Entries.Insert(new CF_ModStorageData<vector>(v));
m_MaxIdx++;
}

void _Insert(string s)
{
m_Entries.Insert(new CF_ModStorageData<string>(s));
m_MaxIdx++;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
enum CF_ModStorageDataType
{
INVALID = -1,
BOOL,
INT,
FLOAT,
VECTOR,
STRING

//! TODO
//ARRAY_BOOL,
//ARRAY_INT,
//ARRAY_FLOAT,
//ARRAY_VECTOR,
//ARRAY_STRING
};

class CF_ModStorageDataBase
{
typename ValueType()
{
return typename;
}

string ValueString()
{
return "";
}
};

class CF_ModStorageData<Class T>: CF_ModStorageDataBase
{
T m_Value;

void CF_ModStorageData(T value)
{
m_Value = value;
}

T Get()
{
return m_Value;
}

override typename ValueType()
{
return T;
}

override string ValueString()
{
return string.ToString(m_Value);
}
};
Loading