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

Open
wants to merge 19 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 = 129;

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