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 DBArray for all LDB, LMT, and LMU structs #383

Draft
wants to merge 47 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ea227f7
Remove unessessary template arguments
fmatthew5876 Aug 16, 2020
ff04e6e
ReaderUtil: support StringView
fmatthew5876 Aug 16, 2020
40ccdcc
Add DBString class
fmatthew5876 Aug 16, 2020
657f189
DBString: Dedup strings
fmatthew5876 Aug 16, 2020
3e1b812
Remove hash table and use reference counting
fmatthew5876 Aug 17, 2020
963e7be
DBString - remove ref counting
fmatthew5876 Aug 17, 2020
114ad3c
DBString - allow non-const operations
fmatthew5876 Aug 19, 2020
c9c7dff
WriterLcf Decode - use StringView
fmatthew5876 Aug 16, 2020
40fc32e
Actor: use DBString
fmatthew5876 Aug 16, 2020
f22ac97
Skill - use DBString
fmatthew5876 Aug 16, 2020
c44b159
Item: use DBString
fmatthew5876 Aug 16, 2020
b6b694b
Enemy: use DBString
fmatthew5876 Aug 16, 2020
6d29293
Use DBString for troop name
fmatthew5876 Aug 16, 2020
b7cf336
Terrain use DBString
fmatthew5876 Aug 16, 2020
6a9edec
State - use DBString
fmatthew5876 Aug 16, 2020
ba09fbb
Terms: Use DBString
fmatthew5876 Aug 16, 2020
6edfba6
Animation use DBString
fmatthew5876 Aug 16, 2020
3780dfd
Attribute use DBString
fmatthew5876 Aug 16, 2020
7e1bdd7
Chipset use DBString
fmatthew5876 Aug 16, 2020
b3f0a9e
Recode - use StringView
fmatthew5876 Aug 16, 2020
ba7fdf4
System - use StringView
fmatthew5876 Aug 16, 2020
0ca13fb
Switch, Var, CommonEvent use DBString
fmatthew5876 Aug 16, 2020
e503aff
Class, BattleCommand, BattlerAnim - Use DBString
fmatthew5876 Aug 16, 2020
426578c
Chipset name use DBString
fmatthew5876 Aug 16, 2020
bec1d9a
MapInfo - use DBString
fmatthew5876 Aug 16, 2020
de4480e
EventCommand use DBString
fmatthew5876 Aug 16, 2020
f42d285
MapInfo - use DBString for background name
fmatthew5876 Aug 17, 2020
bbe6127
Map structures - use DBString
fmatthew5876 Aug 17, 2020
828db0d
MoveCommand - use DBString
fmatthew5876 Aug 17, 2020
329c082
ReaderLcf: optimize DBString reading
fmatthew5876 Aug 19, 2020
c37a5af
Remove some includes
fmatthew5876 Aug 19, 2020
7fbebf3
Add DBArray
fmatthew5876 Aug 18, 2020
6087ae4
Add DBArray support to generate script
fmatthew5876 Aug 19, 2020
09d6261
EventCommand: use DBArray
fmatthew5876 Aug 19, 2020
ce04f14
Add support for DBArray to parsing framework
fmatthew5876 Aug 19, 2020
1efc4f6
SizeField - support multiple array types
fmatthew5876 Aug 19, 2020
c663c04
Writer_Lcf support DBArray
fmatthew5876 Aug 19, 2020
2bed443
Reader_Lcf support DBArray
fmatthew5876 Aug 19, 2020
c5dbfac
ReaderStruct - support DBArray
fmatthew5876 Aug 19, 2020
30f657e
Actor use DBArray
fmatthew5876 Aug 19, 2020
4f5cb70
Actor Learning use DBArray
fmatthew5876 Aug 19, 2020
ed9e658
Add DBBitArray
fmatthew5876 Aug 19, 2020
cb8a1f4
DBString - use DBArrayBase allocators
fmatthew5876 Aug 19, 2020
e0f7f9a
DBArrayBase -> DBArrayAlloc static class
fmatthew5876 Aug 19, 2020
9e9b76f
Add support for DBBitArray to reader struct + parsers
fmatthew5876 Aug 19, 2020
adfee98
Add DBBitArray to generator script
fmatthew5876 Aug 19, 2020
998866b
Use DBBitArray
fmatthew5876 Aug 19, 2020
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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_library(lcf)

set(LCF_SOURCES
src/data.cpp
src/dbarray.cpp
src/encoder.cpp
src/ini.cpp
src/inireader.cpp
Expand Down Expand Up @@ -194,6 +195,10 @@ set(LCF_SOURCES

set(LCF_HEADERS
src/lcf/data.h
src/lcf/dbarray.h
src/lcf/dbarrayalloc.h
src/lcf/dbbitarray.h
src/lcf/dbstring.h
src/lcf/encoder.h
src/lcf/enum_tags.h
src/lcf/flag_set.h
Expand Down
5 changes: 5 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ liblcf_la_LDFLAGS = \
-no-undefined
liblcf_la_SOURCES = \
src/data.cpp \
src/dbarray.cpp \
src/encoder.cpp \
src/ini.cpp \
src/inireader.cpp \
Expand Down Expand Up @@ -201,6 +202,10 @@ liblcf_la_SOURCES = \

lcfinclude_HEADERS = \
src/lcf/data.h \
src/lcf/dbarray.h \
src/lcf/dbarrayalloc.h \
src/lcf/dbbitarray.h \
src/lcf/dbstring.h \
src/lcf/encoder.h \
src/lcf/enum_tags.h \
src/lcf/flag_set.h \
Expand Down
438 changes: 219 additions & 219 deletions generator/csv/fields.csv

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions generator/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
'Int16': 'int16_t',
'Int32': 'int32_t',
'String': 'std::string',
'DBString': 'DBString',
'DBBitArray': 'DBBitArray',
}

# Additional Jinja 2 functions
Expand Down Expand Up @@ -70,6 +72,9 @@ def cpp_type(ty, prefix=True):
m = re.match(r'(Vector|Array)<(.*)>', ty)
if m:
return 'std::vector<%s>' % cpp_type(m.group(2), prefix)
m = re.match(r'DBArray<(.*)>', ty)
if m:
return 'DBArray<%s>' % cpp_type(m.group(1), prefix)

m = re.match(r'Ref<(.*):(.*)>', ty)
if m:
Expand Down Expand Up @@ -100,7 +105,7 @@ def pod_default(field):
ftype = field.type

# Not a POD, no default
if dfl == '' or dfl == '\'\'' or ftype.startswith('Vector') or ftype.startswith('Array'):
if dfl == '' or dfl == '\'\'' or ftype.startswith('Vector') or ftype.startswith('Array') or ftype.startswith('DBArray') or ftype.startswith('DBBitArray'):
return ""

if ftype == 'Boolean':
Expand Down Expand Up @@ -164,6 +169,12 @@ def struct_headers(ty, header_map):
if ty == 'String':
return ['<string>']

if ty == 'DBString':
return ['"lcf/dbstring.h"']

if ty == 'DBBitArray':
return ['"lcf/dbbitarray.h"']

if ty in int_types or ty == "DatabaseVersion":
return ['<stdint.h>']

Expand Down Expand Up @@ -191,6 +202,10 @@ def struct_headers(ty, header_map):
if m:
return ['<vector>'] + struct_headers(m.group(2), header_map)

m = re.match(r'DBArray<(.*)>', ty)
if m:
return ['<lcf/dbarray.h>'] + struct_headers(m.group(1), header_map)

header = header_map.get(ty)
if header is not None:
return ['"lcf/rpg/%s.h"' % header]
Expand Down Expand Up @@ -336,7 +351,7 @@ def needs_ctor(struct_name):
for method, hdrs in setup[struct_name])

def type_is_array(ty):
return re.match(r'(Vector|Array)<(.*)>', ty)
return re.match(r'(Vector|Array|DBArray)<(.*)>', ty) or ty == "DBBitArray"

def is_monotonic_from_0(enum):
expected = 0
Expand Down
91 changes: 91 additions & 0 deletions src/dbarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "lcf/dbarrayalloc.h"
#include "lcf/dbarray.h"
#include "lcf/dbstring.h"
#include <cassert>
#include <cstddef>

//#define LCF_DEBUG_DBARRAY

#ifdef LCF_DEBUG_DBARRAY
#include <iostream>
#endif

namespace lcf {

constexpr DBArrayAlloc::size_type DBArrayAlloc::_empty_buf;
constexpr DBString::size_type DBString::npos;

static ptrdiff_t HeaderSize(size_t align) {
return std::max(sizeof(DBArrayAlloc::size_type), align);
}

static size_t AllocSize(size_t size, size_t align) {
return HeaderSize(align) + size;
}

static void* Adjust(void* p, ptrdiff_t off) {
return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(p) + off);
}

void* DBArrayAlloc::alloc(size_type size, size_type field_size, size_type align) {
if (size == 0) {
return empty_buf();
}
assert(align <= alignof(std::max_align_t));
auto* raw = ::operator new(AllocSize(size, align));
auto* p = Adjust(raw, HeaderSize(align));
*get_size_ptr(p) = field_size;
#ifdef LCF_DEBUG_DBARRAY
std::cout << "DBArray: Allocated"
<< " size=" << size
<< " field_size=" << *get_size_ptr(p)
<< " align=" << align
<< " ptr=" << raw
<< " adjusted=" << p
<< std::endl;
#endif
return p;
}

void DBArrayAlloc::free(void* p, size_type align) noexcept {
assert(p != nullptr);
if (p != empty_buf()) {
auto* raw = Adjust(p, -HeaderSize(align));
#ifdef LCF_DEBUG_DBARRAY
std::cout << "DBArray: Free"
<< " align=" << align
<< " ptr=" << raw
<< " adjusted=" << p
<< " field_size=" << *get_size_ptr(p)
<< std::endl;
#endif
::operator delete(raw);
}
}

char* DBString::construct_z(const char* s, size_t len) {
auto* p = alloc(len);
if (len) {
std::memcpy(p, s, len + 1);
}
return p;
}

char* DBString::construct_sv(const char* s, size_t len) {
auto* p = alloc(len);
if (len) {
std::memcpy(p, s, len);
p[len] = '\0';
}
return p;
}

DBString& DBString::operator=(const DBString& o) {
if (this != &o) {
destroy();
_storage = construct_z(o.data(), o.size());
}
return *this;
}

} // namespace lcf
22 changes: 11 additions & 11 deletions src/generated/lcf/rpg/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#define LCF_RPG_ACTOR_H

// Headers
#include <lcf/dbarray.h>
#include <stdint.h>
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/rpg/equipment.h"
#include "lcf/rpg/learning.h"
#include "lcf/rpg/parameters.h"
Expand All @@ -31,16 +31,16 @@ namespace rpg {
public:
void Setup();
int ID = 0;
std::string name;
std::string title;
std::string character_name;
DBString name;
DBString title;
DBString character_name;
int32_t character_index = 0;
bool transparent = false;
int32_t initial_level = 1;
int32_t final_level = -1;
bool critical_hit = true;
int32_t critical_hit_chance = 30;
std::string face_name;
DBString face_name;
int32_t face_index = 0;
bool two_weapon = false;
bool lock_equipment = false;
Expand All @@ -56,12 +56,12 @@ namespace rpg {
int32_t battle_x = 220;
int32_t battle_y = 120;
int32_t battler_animation = 1;
std::vector<Learning> skills;
DBArray<Learning> skills;
bool rename_skill = false;
std::string skill_name;
std::vector<uint8_t> state_ranks;
std::vector<uint8_t> attribute_ranks;
std::vector<int32_t> battle_commands;
DBString skill_name;
DBArray<uint8_t> state_ranks;
DBArray<uint8_t> attribute_ranks;
DBArray<int32_t> battle_commands;
};

inline bool operator==(const Actor& l, const Actor& r) {
Expand Down
6 changes: 3 additions & 3 deletions src/generated/lcf/rpg/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#define LCF_RPG_ANIMATION_H

// Headers
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include "lcf/rpg/animationframe.h"
#include "lcf/rpg/animationtiming.h"
Expand Down Expand Up @@ -48,8 +48,8 @@ namespace rpg {
);

int ID = 0;
std::string name;
std::string animation_name;
DBString name;
DBString animation_name;
bool large = false;
std::vector<AnimationTiming> timings;
int32_t scope = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Headers
#include <stdint.h>
#include <string>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand All @@ -36,7 +36,7 @@ namespace rpg {
);

int ID = 0;
std::string name;
DBString name;
int32_t type = 0;
int32_t a_rate = 300;
int32_t b_rate = 200;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/battlecommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define LCF_RPG_BATTLECOMMAND_H

// Headers
#include <string>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace rpg {
);

int ID = 0;
std::string name;
DBString name;
int32_t type = 0;
};
inline std::ostream& operator<<(std::ostream& os, BattleCommand::Type code) {
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/battleranimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#define LCF_RPG_BATTLERANIMATION_H

// Headers
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include "lcf/rpg/battleranimationextension.h"
#include <ostream>
Expand All @@ -34,7 +34,7 @@ namespace rpg {
};

int ID = 0;
std::string name;
DBString name;
int32_t speed = 0;
std::vector<BattlerAnimationExtension> base_data;
std::vector<BattlerAnimationExtension> weapon_data;
Expand Down
6 changes: 3 additions & 3 deletions src/generated/lcf/rpg/battleranimationextension.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Headers
#include <stdint.h>
#include <string>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand All @@ -36,8 +36,8 @@ namespace rpg {
);

int ID = 0;
std::string name;
std::string battler_name;
DBString name;
DBString battler_name;
int32_t battler_index = 0;
int32_t animation_type = 0;
int32_t animation_id = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/generated/lcf/rpg/chipset.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// Headers
#include <stdint.h>
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand All @@ -39,8 +39,8 @@ namespace rpg {
Chipset();
void Init();
int ID = 0;
std::string name;
std::string chipset_name;
DBString name;
DBString chipset_name;
std::vector<int16_t> terrain_data;
std::vector<uint8_t> passable_data_lower;
std::vector<uint8_t> passable_data_upper;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// Headers
#include <stdint.h>
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/rpg/learning.h"
#include "lcf/rpg/parameters.h"
#include <ostream>
Expand All @@ -29,7 +29,7 @@ namespace rpg {
class Class {
public:
int ID = 0;
std::string name;
DBString name;
bool two_weapon = false;
bool lock_equipment = false;
bool auto_battle = false;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/commonevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#define LCF_RPG_COMMONEVENT_H

// Headers
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include "lcf/rpg/eventcommand.h"
#include <ostream>
Expand All @@ -34,7 +34,7 @@ namespace rpg {
};

int ID = 0;
std::string name;
DBString name;
int32_t trigger = 0;
bool switch_flag = false;
int32_t switch_id = 1;
Expand Down
Loading