-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUtil.cs
255 lines (226 loc) · 10.4 KB
/
Util.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.BellsAndWhistles;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace StardewValleyMP
{
class Util
{
public static bool UsingMono
{
get { return Type.GetType("Mono.Runtime") != null; }
}
public static Texture2D WHITE_1X1;
public static void drawStr(string str, float x, float y, Color col, float alpha = 1, bool smallFont = true)
{
for ( int i = 0; i < str.Length; ++i )
{
if ( !Game1.smallFont.Characters.Contains( str[ i ] ) )
{
str = str.Remove(i, 1).Insert(i, "?");
}
}
/*SpriteBatch b = Game1.spriteBatch;
b.DrawString(Game1.smallFont, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(2f, 2f), Game1.textShadowColor * alpha);
b.DrawString(Game1.smallFont, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(0f, 2f), Game1.textShadowColor * alpha);
b.DrawString(Game1.smallFont, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(2f, 0f), Game1.textShadowColor * alpha);
b.DrawString(Game1.smallFont, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)), col * 0.9f * alpha);*/
SpriteBatch b = Game1.spriteBatch;
Color inverted = new Color(255 - col.R, 255 - col.G, 255 - col.B);
SpriteFont font = smallFont ? Game1.smallFont : Game1.dialogueFont;
b.DrawString(font, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(-2f, 0f), inverted * alpha * 0.8f);
b.DrawString(font, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(2f, 0f), inverted * alpha * 0.8f);
b.DrawString(font, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(0f, 2f), inverted * alpha * 0.8f);
b.DrawString(font, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)) + new Vector2(0f, -2f), inverted * alpha * 0.8f);
b.DrawString(font, str, new Vector2((float)(x + Game1.tileSize / 4), (float)(y + Game1.tileSize / 4 + 4)), col * 0.9f * alpha);
}
// http://stackoverflow.com/a/17546909
public static bool stringDialog( string title, ref string input )
{
System.Drawing.Size size = new System.Drawing.Size(200, 70);
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
inputBox.Text = title;
System.Windows.Forms.TextBox textBox = new TextBox();
textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
textBox.Location = new System.Drawing.Point(5, 5);
textBox.Text = input;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new System.Drawing.Size(75, 23);
okButton.Text = "&OK";
okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39);
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
cancelButton.Name = "cancelButton";
cancelButton.Size = new System.Drawing.Size(75, 23);
cancelButton.Text = "&Cancel";
cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39);
inputBox.Controls.Add(cancelButton);
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
DialogResult result = inputBox.ShowDialog();
input = textBox.Text;
return ( result == DialogResult.OK );
}
public static bool yesNoDialog( string title, string text )
{
return (MessageBox.Show(title, text, MessageBoxButtons.YesNo) == DialogResult.Yes);
}
// http://stackoverflow.com/a/22456034
public static string serialize< T >( T obj )
{
using ( MemoryStream stream = new MemoryStream() )
{
XmlSerializer serializer = new XmlSerializer( obj.GetType() );
serializer.Serialize(stream, obj);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
public static T deserialize< T >( string str )
{
int beg = str.IndexOf('<');
//string root = str.Substring(beg + 1, str.IndexOf(" xmlns") - beg - 1);
XmlSerializer serializer = new XmlSerializer(typeof(T)/*, new XmlRootAttribute( root )*/);
using ( TextReader reader = new StringReader( str ) )
{
return ( T )serializer.Deserialize(reader);
}
}
// http://stackoverflow.com/questions/1879395/how-to-generate-a-stream-from-a-string
public static Stream stringStream( string str )
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(str);
writer.Flush();
stream.Position = 0;
return stream;
}
// http://stackoverflow.com/questions/3303126/how-to-get-the-value-of-private-field-in-c
public static object GetInstanceField(Type type, object instance, string fieldName)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
FieldInfo field = type.GetField(fieldName, bindFlags);
return field.GetValue(instance);
}
public static void SetInstanceField(Type type, object instance, string fieldName, object value)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
FieldInfo field = type.GetField(fieldName, bindFlags);
field.SetValue(instance, value);
}
public static object GetStaticField(Type type, string fieldName)
{
BindingFlags bindFlags = BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
FieldInfo field = type.GetField(fieldName, bindFlags);
return field.GetValue(null);
}
public static void SetStaticField(Type type, string fieldName, object value)
{
BindingFlags bindFlags = BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
FieldInfo field = type.GetField(fieldName, bindFlags);
field.SetValue(null, value);
}
public static void CallStaticMethod(Type type, string name, object[] args)
{
// TODO: Support method overloading
BindingFlags bindFlags = BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
MethodInfo func = type.GetMethod(name, bindFlags);
func.Invoke(null, args);
}
public static void CallInstanceMethod(Type type, object instance, string name, object[] args)
{
// TODO: Support method overloading
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
MethodInfo func = type.GetMethod(name, bindFlags);
func.Invoke(instance, args);
}
public static bool AreEqual< TKey, TVal >( Dictionary< TKey, TVal > a, Dictionary< TKey, TVal > b,
Func< TVal, TVal, bool > comp = null )
{
if (a.Count != b.Count)
return false;
else
{
List<TKey> keys = a.Keys.ToList();
foreach (TKey key in b.Keys)
{
if (!keys.Contains(key))
{
// A key is in the new but not old.
return false;
}
else if ( ( comp == null && !a[ key ].Equals( b[ key ] ) ) || ( comp != null && !comp( a[ key ], b[ key ] ) ) )
{
return false;
}
else
{
keys.Remove(key);
}
}
// A key was in the old but not the new.
if (keys.Count > 0)
return false;
}
return true;
}
// Compression utils:
/// <summary>
/// Compresses byte array to new byte array.
/// </summary>
public static byte[] Compress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionLevel.Optimal))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
public static byte[] Decompress(byte[] gzip)
{
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
}
}