summaryrefslogtreecommitdiff
path: root/common/library.h
blob: 0300e04dd3527df9854b63ef544684921dc6648e (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
#ifndef _LIBRARY_H_
#define _LIBRARY_H_

#include "defines.h"
#include "str.h"
#include "array.h"

class File;
class Texture;
class PieceInfo;

#define LC_PIECESLIB_MAXGROUPS    32

typedef struct
{
	String Name;
	String Keywords;
} PiecesLibraryCategory;

class PiecesLibrary
{
public:
  PiecesLibrary ();
  ~PiecesLibrary ();

  const char* GetLibraryPath() const
    { return m_LibraryPath; }
  int GetPieceCount () const
    { return m_nPieceCount; }
  int GetTextureCount () const
    { return m_nTextureCount; }
  int GetGroupCount () const
    { return m_nGroupCount; }
  String& GetGroup (int i)
    { return m_strGroups[i]; }
	bool NeedsReload () const
		{ return m_bNeedsReload; }

	// Categories.
	void GetCategoryEntries(int CategoryIndex, PtrArray<PieceInfo>& SinglePieces, PtrArray<PieceInfo>& GroupedPieces);
	void GetPatternedPieces(PieceInfo* Parent, PtrArray<PieceInfo>& Pieces);
	void SetCategory(int Index, const String& Name, const String& Keywords);
	void AddCategory(const String& Name, const String& Keywords);
	void RemoveCategory(int Index);

	const char* GetCategoryName(int Index) const
		{ return m_Categories[Index].Name; }

	const char* GetCategoryKeywords(int Index) const
		{ return m_Categories[Index].Keywords; }

	int GetNumCategories() const
		{ return m_Categories.GetSize(); }

	int FindCategoryIndex(const String& CategoryName) const
	{
		for (int i = 0; i < m_Categories.GetSize(); i++)
			if (m_Categories[i].Name == CategoryName)
				return i;

		return -1;
	}

	void CheckReload ();
  bool Load (const char* libpath);
  void Unload ();
	bool LoadGroupConfig (const char* Filename);

	// Search for pieces.
  PieceInfo* FindPieceInfo (const char* name) const;
  PieceInfo* GetPieceInfo (int index) const;
  int GetPieceIndex (PieceInfo *pInfo) const;
  Texture* FindTexture (const char* name) const;
  Texture* GetTexture (int index) const;

	// File operations.
  bool DeletePieces (char** names, int numpieces);
  bool LoadUpdate (const char* update);
	bool DeleteTextures (char** Names, int NumTextures);
	bool ImportTexture (const char* Name);
	bool ImportLDrawPiece (const char* Filename);

	static unsigned long GetDefaultPieceGroup (const char* name);

protected:
  char m_LibraryPath[LC_MAXPATH];	// path to the library files

	int m_nMovedCount;       // number of moved pieces
  char* m_pMovedReference; // moved pieces list
	int m_nPieceCount;       // number of pieces
	PieceInfo* m_pPieceIdx;	 // pieces array
	int m_nTextureCount;     // number of textures
	Texture* m_pTextures;    // textures array

	// Categories.
	ObjArray<PiecesLibraryCategory> m_Categories;

	// Groups stuff
	int m_nGroupCount;
	String m_strGroups[LC_PIECESLIB_MAXGROUPS];
	char m_GroupsFile[LC_MAXPATH];

	bool m_bNeedsReload;  // if the library files were changed and they need to be reloaded

	bool ValidatePiecesFile (File& IdxFile, File& BinFile) const;
	bool ValidateTexturesFile (File& IdxFile, File& BinFile) const;

	// File headers
	static const char PiecesBinHeader[32];
	static const char PiecesIdxHeader[32];
	static const int PiecesFileVersion;
	static const char TexturesBinHeader[32];
	static const char TexturesIdxHeader[32];
	static const int TexturesFileVersion;
};




// ============================================================================

// This should be cleaned and moved to the PiecesLibrary class
typedef struct connection_s
{
	unsigned char type;
	float pos[3];
	float up[3];
	connection_s* next;
} connection_t;

typedef struct group_s
{
	connection_t* connections[5];
	void* drawinfo;
	unsigned long infosize;
	group_s* next;
} group_t;

typedef struct lineinfo_s
{
	unsigned char type;
	unsigned char color;
	float points[12];
	lineinfo_s* next;
} lineinfo_t;

typedef struct texture_s
{
	float points[20];
	unsigned char color;
	char name[9];
	texture_s* next;
} texture_t;

typedef struct
{
	float* verts;
	unsigned int verts_count;
	bool long_info;
	connection_t* connections;
	group_t* groups;
	texture_t* textures;
	char name[9];
	char description[65];
} LC_LDRAW_PIECE;

bool ReadLDrawPiece(const char* filename, LC_LDRAW_PIECE* piece);
bool SaveLDrawPiece(LC_LDRAW_PIECE* piece);
void FreeLDrawPiece(LC_LDRAW_PIECE* piece);

#endif // _LIBRARY_H_