-
Notifications
You must be signed in to change notification settings - Fork 4
/
EUP Config Helper.cs
335 lines (291 loc) · 13 KB
/
EUP Config Helper.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YobbinCallouts
{
using Rage;
using Rage.Native;
/// <summary>
/// Developed by PNWParksFan
/// Based on code from PieRGud
/// </summary>
public class EUPOutfit
{
public string Description { get; private set; }
public IReadOnlyDictionary<EPedComponents, Component> Components { get; private set; } = new Dictionary<EPedComponents, Component>();
public IReadOnlyDictionary<EProps, Component> Props { get; private set; } = new Dictionary<EProps, Component>();
public EGender PedGender { get; private set; }
/// <summary>
/// Creates a configuration based on specifically configured settings
/// </summary>
/// <param name="description"></param>
/// <param name="gender"></param>
/// <param name="componentConfig"></param>
/// <param name="propConfig"></param>
public EUPOutfit(string description, EGender gender, IDictionary<EPedComponents, Component> componentConfig, IDictionary<EProps, Component> propConfig)
{
PedGender = gender;
Components = componentConfig.ToDictionary(x => x.Key, x => x.Value);
Props = propConfig.ToDictionary(x => x.Key, x => x.Value);
Description = description;
}
/// <summary>Searches a specific INI file for an outfit name</summary>
/// <param name="INI">The INI file to search in</param>
/// <param name="sectionName">The outfit name to search for</param>
/// <param name="success">Whether the outfit was found</param>
public EUPOutfit(InitializationFile INI, string sectionName, out bool success)
{
this.Description = sectionName;
success = _initializePedFromINI(INI, sectionName);
}
/// <summary>
/// Looks in EUP config files for an outfit with the specified name.
/// Looks in wardrobe.ini first, then presetoutfits.ini if not found
/// </summary>
/// <param name="sectionName">The config name to search for</param>
/// <param name="success">If a matching configuration was found</param>
public EUPOutfit(string sectionName, out bool success)
{
this.Description = sectionName;
InitializationFile wardrobeINI = new InitializationFile(@"Plugins\EUP\wardrobe.ini");
InitializationFile presetsINI = new InitializationFile(@"Plugins\EUP\presetoutfits.ini");
bool wardrobeSuccess = _initializePedFromINI(wardrobeINI, sectionName);
Game.LogTrivial("Found " + sectionName + " in wardrobe: " + wardrobeSuccess);
if (wardrobeSuccess)
{
success = true;
}
else
{
bool presetSuccess = _initializePedFromINI(presetsINI, sectionName);
success = presetSuccess;
Game.LogTrivial("Found " + sectionName + " in presets: " + presetSuccess);
}
}
private bool _initializePedFromINI(InitializationFile INI, string sectionName)
{
Description = sectionName;
Game.LogTrivial("Reading ped configuration for " + sectionName + " from INI file " + INI.FileName);
PedGender = INI.ReadEnum<EGender>(sectionName, "Gender", EGender.UNKNOWN);
Dictionary<EPedComponents, Component> newProps = new Dictionary<EPedComponents, Component>();
Components = _readComponentSettingsFromINI<EPedComponents>(INI, sectionName).ToDictionary(x => (EPedComponents)x.Key, x => x.Value);
Props = _readComponentSettingsFromINI<EProps>(INI, sectionName).ToDictionary(x => (EProps)x.Key, x => x.Value);
return INI.DoesSectionExist(sectionName);
}
private bool _convertIniEntryToVariations(string part, string compSetting, out Tuple<int, int> variation)
{
// Game.LogDebug("Attempting to convert " + compSetting + " to component entry");
variation = Tuple.Create(0, 0);
string[] compSettings = compSetting.Split(':');
if (compSettings.Length != 2)
{
Game.LogTrivial("Setting for " + part + " does not match expected format of num:num");
return false;
}
int drawableVar = 0;
int textureVar = 0;
bool success = true;
success = success && int.TryParse(compSettings[0], out drawableVar);
success = success && int.TryParse(compSettings[1], out textureVar);
if (!success)
{
Game.LogTrivial("Setting for " + part + " contained invalid non-int values");
return false;
}
else
{
// Decrease by 1 because EUP INI uses 1-indexed but game used 0-indexed
drawableVar--;
textureVar--;
Game.LogTrivial("Successfully parsed component " + part + " to variation " + drawableVar + ", texture " + textureVar);
variation = Tuple.Create(drawableVar, textureVar);
return true;
}
}
private IReadOnlyDictionary<Enum, Component> _readComponentSettingsFromINI<T>(InitializationFile INI, string sectionName)
{
Dictionary<Enum, Component> newComponents = new Dictionary<Enum, Component>();
foreach (Enum component in Enum.GetValues(typeof(T)))
{
string compSetting = INI.ReadString(sectionName, component.ToString(), null);
if (compSetting == null)
{
Game.LogTrivial("No setting found for " + component);
continue;
}
Tuple<int, int> variation;
bool success = _convertIniEntryToVariations(component.ToString(), compSetting, out variation);
newComponents.Add(component, new Component(component, variation.Item1, variation.Item2));
}
return newComponents;
}
private Model _getModelForGender()
{
switch (PedGender)
{
case EGender.Male:
return new Model("MP_M_FREEMODE_01");
case EGender.Female:
return new Model("MP_F_FREEMODE_01");
default:
return new Model("ERROR_INVALID_GENDER");
}
}
/// <summary>
/// Applies this outfit to the player's ped
/// </summary>
/// <param name="allowChangeModel">Whether to allow changing the player's model.
/// This will cause Game.LocalPlayer.Character to return a new ped, force the player out of any vehicle, and clear all tasks.</param>
/// <param name="allowChangeModelInVehicle">If true and the model must be changed, the new player ped will be warped back into their previous
/// vehicle in the same seat as before. If false, configuration will fail if the model must be changed and the ped is in a vehicle.</param>
/// <param name="restoreWeapons">If the model must be changed, restores the player's weapon inventory after changing the model.</param>
/// <returns>True if the configuration is successful</returns>
public bool ApplyOutfitToPlayer(bool allowChangeModel, bool allowChangeModelInVehicle = true, bool restoreWeapons = true, bool randomizeFace = false)
{
Model requiredModel = _getModelForGender();
Vehicle prevVehicle = Game.LocalPlayer.Character.CurrentVehicle;
int prevSeat = -1;
var inventoryBefore = Game.LocalPlayer.Character.Inventory.Weapons.ToDictionary(w => w.Hash, w => w.Ammo);
if (Game.LocalPlayer.Model != requiredModel)
{
Game.LogTrivial("Player model " + Game.LocalPlayer.Model.Name + " does not match outfit model of " + requiredModel.Name);
if (allowChangeModel)
{
if (prevVehicle && !allowChangeModelInVehicle)
{
Game.LogTrivial("Cannot change player model in vehicle");
return false;
}
}
else
{
Game.LogTrivial("Cannot apply outfit because models do not match");
return false;
}
Game.LogTrivial("Changing ped model to " + requiredModel.Name);
if (!requiredModel.IsValid)
{
Game.LogTrivial("Unable to change ped model to " + requiredModel.Name + " - model may be invalid or gender may not have been specified");
return false;
}
Game.LocalPlayer.Model = requiredModel;
GameFiber.StartNew(delegate
{
if (prevVehicle)
{
Game.LocalPlayer.Character.WarpIntoVehicle(prevVehicle, prevSeat);
}
GameFiber.Sleep(300);
if (restoreWeapons)
{
foreach (var wpn in inventoryBefore)
{
Game.LogTrivial("Restoring weapon " + wpn.Key.ToString());
Game.LocalPlayer.Character.Inventory.GiveNewWeapon(wpn.Key, wpn.Value, false);
GameFiber.Yield();
}
}
});
}
return ApplyOutfitToPed(Game.LocalPlayer.Character, randomizeFace);
}
public bool ApplyOutfitToPed(Ped ped, bool randomizeFace)
{
if (!ped)
{
Game.LogTrivial("Ped is invalid or does not exist");
return false;
}
else if (ped.Model != _getModelForGender())
{
Game.LogTrivial("Ped model " + ped.Model.Name + " does not match requirement for specified gender " + PedGender.ToString());
return false;
}
Game.LogTrivial("Applying outfit " + Description + " to ped");
// Clear props before starting
NativeFunction.Natives.CLEAR_ALL_PED_PROPS(ped);
// Assign all specified props
foreach (var prop in Props)
{
NativeFunction.Natives.SET_PED_PROP_INDEX(ped, (int)prop.Key, prop.Value.DrawableVariation, prop.Value.TextureVariation, 0);
}
// Assign all known components
foreach (var comp in Components)
{
Game.LogTrivial("Setting component " + (int)comp.Key + " (" + comp.Key.ToString() + ") to model " + comp.Value.DrawableVariation + ", texture " + comp.Value.TextureVariation);
ped.SetVariation((int)comp.Key, comp.Value.DrawableVariation, comp.Value.TextureVariation);
}
if (randomizeFace)
{
Game.LogTrivial("Randomizing face");
RandomCharacter.RandomizeCharacter(ped);
}
return true;
}
/// <summary>
/// Spawns a new ped and configures with the loaded outfit
/// </summary>
/// <param name="spawnPosition"></param>
/// <param name="heading"></param>
/// <param name="success">Whether the ped was successfully spawned and configured. May return false if a ped was created but could not be configured.</param>
/// <returns>The ped created</returns>
public Ped Spawn(Vector3 spawnPosition, float heading, out bool success, bool randomizeFace = true)
{
Ped ped;
Model pedModel = _getModelForGender();
if (!pedModel.IsValid || !pedModel.IsPed)
{
Game.LogTrivial("No valid ped model found for specified gender");
success = false;
return null;
}
else
{
Game.LogTrivial("Spawning " + pedModel.Name + " at " + spawnPosition.ToString());
ped = new Ped(pedModel, spawnPosition, heading);
success = ApplyOutfitToPed(ped, randomizeFace);
return ped;
}
}
}
public struct Component
{
public int DrawableVariation { get; private set; }
public int TextureVariation { get; private set; }
public Component(Enum component, int drawableVariation, int textureVariation)
{
this.DrawableVariation = drawableVariation;
this.TextureVariation = textureVariation;
}
}
public enum EGender
{
Male,
Female,
UNKNOWN
}
public enum EProps
{
Hat,
Glasses,
Ear,
Watch = 6,
}
public enum EPedComponents
{
Head,
Mask,
Hair,
UpperSkin,
Pants,
Parachute,
Shoes,
Accessories,
UnderCoat,
Armor,
Decal,
Top
}
}