-
Notifications
You must be signed in to change notification settings - Fork 0
/
STSerializableObject.cs
161 lines (140 loc) · 3.8 KB
/
STSerializableObject.cs
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
/*******************************************************************************
*
* Space Trader for Windows 1.00
*
* Copyright (C) 2016 Keith Banner, All Rights Reserved
*
* Port to Windows by David Pierron & Jay French
* Original coding by Pieter Spronck, Sam Anderson, Samuel Goldstein, Matt Lee
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* If you'd like a copy of the GNU General Public License, go to
* http://www.gnu.org/copyleft/gpl.html.
*
* You can contact the author at [email protected]
*
******************************************************************************/
using System.Collections;
namespace SpaceTrader
{
public abstract class STSerializableObject
{
#region Methods
public STSerializableObject()
{
}
public STSerializableObject(Hashtable hash)
{
}
/*
* Types currently supported:
* CrewMember
* Gadget
* HighScoreRecord
* Shield
* StarSystem
* Weapon
*
* If an array of a type not listed is converted using ArrayToArrayList, the type
* needs to be added here.
*/
public static STSerializableObject[] ArrayListToArray(ArrayList list, string type)
{
STSerializableObject[] array = null;
if (list != null)
{
switch (type)
{
case "CrewMember":
array = new CrewMember[list.Count];
break;
case "Gadget":
array = new Gadget[list.Count];
break;
case "HighScoreRecord":
array = new HighScoreRecord[list.Count];
break;
case "Shield":
array = new Shield[list.Count];
break;
case "StarSystem":
array = new StarSystem[list.Count];
break;
case "Weapon":
array = new Weapon[list.Count];
break;
}
for (int index = 0; index < list.Count; index++)
{
Hashtable hash = (Hashtable)list[index];
STSerializableObject obj = null;
if (hash != null)
{
switch (type)
{
case "CrewMember":
obj = new CrewMember(hash);
break;
case "Gadget":
obj = new Gadget(hash);
break;
case "HighScoreRecord":
obj = new HighScoreRecord(hash);
break;
case "Shield":
obj = new Shield(hash);
break;
case "StarSystem":
obj = new StarSystem(hash);
break;
case "Weapon":
obj = new Weapon(hash);
break;
}
}
array[index] = obj;
}
}
return array;
}
public static int[] ArrayListToIntArray(ArrayList list)
{
int[] array = new int[list.Count];
for (int index = 0; index < array.Length; index++)
array[index] = (int)list[index];
return array;
}
public static ArrayList ArrayToArrayList(STSerializableObject[] array)
{
ArrayList list = null;
if (array != null)
{
list = new ArrayList();
foreach (STSerializableObject obj in array)
list.Add(obj == null ? null : obj.Serialize());
}
return list;
}
public static object GetValueFromHash(Hashtable hash, string key)
{
return GetValueFromHash(hash, key, null);
}
public static object GetValueFromHash(Hashtable hash, string key, object defaultValue)
{
return hash.ContainsKey(key) ? hash[key] : defaultValue;
}
public virtual Hashtable Serialize()
{
return new Hashtable();
}
#endregion
}
}