-
Notifications
You must be signed in to change notification settings - Fork 7
/
PresetManager.cs
370 lines (333 loc) · 16.7 KB
/
PresetManager.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JsonFx.Json;
using UnityEngine;
using CM3D2.AlwaysColorChangeEx.Plugin.Data;
using CM3D2.AlwaysColorChangeEx.Plugin.Util;
using CM3D2.AlwaysColorChangeEx.Plugin.Util.Json;
namespace CM3D2.AlwaysColorChangeEx.Plugin {
/// <summary>
/// プリセットマネージャクラス
/// </summary>
public class PresetManager {
private readonly Settings _settings = Settings.Instance;
private readonly MaidHolder _holder = MaidHolder.Instance;
private readonly TextureModifier _texModifier = TextureModifier.Instance;
private readonly FileUtilEx _fileUtil = FileUtilEx.Instance;
public string GetPresetFilepath(string presetName) {
return Path.Combine(_settings.presetDirPath, presetName + FileConst.EXT_JSON);
}
public PresetData Load(string fileName) {
// ファイル読み込み
try {
using (var fs = File.OpenRead(fileName)) {
var reader = new JsonReader(fs);
return (PresetData)reader.Deserialize(typeof(PresetData));
}
} catch (Exception e) {
LogUtil.Log("ACCプリセットの読み込みに失敗しました", e);
return null;
}
}
public void Save(string fileName, string presetName, Dictionary<string, bool> dDelNodes) {
var maid = _holder.CurrentMaid;
// カレントのメイドデータからプリセットデータを抽出
var preset = new PresetData {name = presetName};
foreach (var slotInfo in ACConstants.SlotNames.Values) {
if (!slotInfo.enable) continue;
var slot = maid.body0.GetSlot((int)slotInfo.Id);
// マスク情報を抽出
SlotState maskState;
if (slot.obj == null) {
maskState = SlotState.NotLoaded;
} else if (!slot.boVisible) {
maskState = SlotState.Masked;
} else {
maskState = SlotState.Displayed;
}
var materialList = _holder.GetMaterials(slot);
if (materialList.Length == 0) continue;
var slotItem = new CCSlot(slotInfo.Id) {mask = maskState};
foreach (var material in materialList) {
var type = ShaderType.Resolve(material.shader.name);
if (type == ShaderType.UNKNOWN) continue;
var cmat = new CCMaterial(material, type);
slotItem.Add(cmat);
foreach (var texProp in type.texProps) {
var tex2D = material.GetTexture(texProp.propId) as Texture2D;
if (tex2D == null || string.IsNullOrEmpty(tex2D.name)) continue;
var ti = new TextureInfo();
cmat.Add(ti);
ti.propName = texProp.keyName;
ti.texFile = tex2D.name;
var fp = _texModifier.GetFilter(maid, slotInfo.Id.ToString(), material.name, tex2D.name);
if (fp != null && !fp.HasNotChanged()) ti.filter = new TexFilter(fp);
#if UNITY_5_6_OR_NEWER
var offset = material.GetTextureOffset(texProp.propId);
#else
var offset = material.GetTextureOffset(texProp.keyName);
#endif
if (Math.Abs(offset.x) > ConstantValues.EPSILON_3) {
ti.offsetX = offset.x;
}
if (Math.Abs(offset.y) > ConstantValues.EPSILON_3) {
ti.offsetY = offset.y;
}
#if UNITY_5_6_OR_NEWER
var scale = material.GetTextureScale(texProp.propId);
#else
var scale = material.GetTextureScale(texProp.keyName);
#endif
if (Math.Abs(scale.x) > ConstantValues.EPSILON_3) {
ti.scaleX = scale.x;
}
if (Math.Abs(scale.y) > ConstantValues.EPSILON_3) {
ti.scaleY = scale.y;
}
}
}
preset.slots.Add(slotItem);
}
for (var i = TypeUtil.BODY_START; i <= TypeUtil.BODY_END; i++) {
var mpn = (MPN)Enum.ToObject(typeof(MPN), i);
var mp = maid.GetProp(mpn);
if (mp == null) continue;
// 身体パラメータ
if (mp.type == 1 || mp.type == 2) {
preset.mpnvals.Add(new CCMPNValue(mpn, mp.value, mp.min, mp.max));
continue;
}
// スロットアイテム
if (mp.type == 3 && mp.nFileNameRID != 0) {
preset.mpns.Add(new CCMPN(mpn, mp.strFileName));
}
}
// FOLDER_BODYは自動で0にリセットされるためプリセットの保持する必要はない
// for (var i = MPN_TYPE_RANGE.FOLDER_BODY_START; i <= MPN_TYPE_RANGE.FOLDER_BODY_END; i++) {
// var mpn = (MPN)Enum.ToObject(typeof(MPN), i);
// var mp = maid.GetProp(mpn);
// if (mp == null || mp.nFileNameRID == 0) continue;
// preset.mpns.Add(new CCMPN(mpn, mp.strFileName));
// }
for (var i = TypeUtil.WEAR_START; i <= TypeUtil.WEAR_END; i++) {
var mpn = (MPN)Enum.ToObject(typeof(MPN), i);
var mp = maid.GetProp(mpn);
if (mp != null && mp.nFileNameRID != 0) {
preset.mpns.Add(new CCMPN(mpn, mp.strFileName));
}
}
// 無限色
for (var pcEnum = TypeUtil.PARTS_COLOR_START; pcEnum <= TypeUtil.PARTS_COLOR_END; pcEnum++) {
var part = maid.Parts.GetPartsColor(pcEnum);
preset.partsColors[pcEnum.ToString()] = new CCPartsColor(part);
}
// 表示ノード
preset.delNodes = new Dictionary<string, bool>(dDelNodes);
LogUtil.Debug("create preset...", fileName);
SavePreset(fileName, preset);
}
public void SavePreset(string fileName, PresetData preset) {
if (File.Exists(fileName)) {
File.Delete(fileName);
}
// ファイル出力
var jws = new JsonWriterSettings {
MaxDepth = 200,
PrettyPrint = true
};
using (var fs = File.OpenWrite(fileName))
using (var writer = new CustomJsonWriter(fs, jws)) {
writer.ignoreNull = true;
writer.Write(preset);
}
LogUtil.Debug("preset saved...", fileName);
}
public void ApplyPresetMPN(Maid maid, PresetData preset, bool applyBody, bool applyWear, bool castoff) {
// 衣装チェンジ
foreach (var mpn in preset.mpns) {
if (!applyBody) {
// bodyのMPNをスキップ
if (TypeUtil.IsBody(mpn.name)) continue;
}
if (!applyWear) {
// wearのMPNをスキップ
if (TypeUtil.IsWear(mpn.name)) continue;
}
// menuファイルが存在しない場合はスキップ
if (!_fileUtil.Exists(mpn.filename)) continue;
var prop = maid.GetProp(mpn.name);
if (mpn.filename.Equals(prop.strFileName, StringComparison.OrdinalIgnoreCase)) {
LogUtil.Debug("apply preset skip. mpn:", mpn.name, ", file:", mpn.filename);
continue;
}
if (mpn.name == MPN.body) {
LogUtil.Log("ACCexプリセットのbodyメニューの適用は現在未対応です。スキップします。", mpn.filename);
continue;
}
if (mpn.filename.EndsWith("_del.menu", StringComparison.OrdinalIgnoreCase)) {
if (castoff) {
// 対象のMPNが空でかつ、指定アイテムが削除アイテムと同一であればスキップ
if (prop.nFileNameRID == 0) {
if (CM3.dicDelItem[mpn.name].Equals(mpn.filename, StringComparison.OrdinalIgnoreCase)) {
continue;
}
}
// LogUtil.Debug("apply prop(del): ", mpn.filename, ", old:", prop.strFileName);
if (SetProp != null) SetProp(maid, mpn.name, mpn.filename, 0);
}
continue;
// } else if (mpn.filename.EndsWith(".mod", StringComparison.OrdinalIgnoreCase)) {
}
// LogUtil.Debug("apply prop: ", mpn.filename, ", old:", prop.strFileName);
if (SetProp != null) SetProp(maid, mpn.name, mpn.filename, 0);
}
}
public void ApplyPresetMPNProp(Maid maid, PresetData preset) {
// 設定プロパティ反映
foreach (var mpn in preset.mpnvals) {
var mp = maid.GetProp(mpn.name);
if (mp != null) {
mp.value = mpn.value;
if (mp.min > mpn.min) { mp.min = mpn.min; }
if (mp.max < mpn.max) { mp.max = mpn.max; }
} else {
LogUtil.Debug("failed to apply MaidProp. mpn:", mpn.name);
}
}
}
public void ApplyPresetMaterial(Maid maid, PresetData preset) {
if (maid == null) maid = _holder.CurrentMaid;
if (maid == null) return;
foreach (var ccslot in preset.slots) {
var slotNo = (int)ccslot.id;
if (slotNo >= maid.body0.goSlot.Count) continue; // スロットがないケースはスキップ
// スロット上のマテリアル番号での判断に変更
var slot = maid.body0.GetSlot(slotNo);
var materials = _holder.GetMaterials(slot);
if (slot.obj == null) {
LogUtil.Debug("slot.obj null. name=", ccslot.id);
}
if (!materials.Any()) continue; // 未装着スロットはスキップ
var slotName = ccslot.id.ToString();
var matNo = -1;
foreach (var cmat in ccslot.materials) {
if (++matNo < materials.Length) {
var m = materials[matNo];
if (cmat.name != m.name) {
LogUtil.DebugF("Material name mismatched. skipping apply preset-slot={0}, matNo={1}, name=({2}<=>{3})",
ccslot.id, matNo, cmat.name, m.name);
continue;
}
cmat.Apply(m);
// テクスチャ適用
var texes = cmat.texList;
if (texes == null) continue;
foreach (var texInfo in texes) {
var tex = m.GetTexture(texInfo.propName);
// テクスチャファイルの変更
if (tex == null || tex.name != texInfo.texFile) {
var filename = texInfo.texFile;
if (filename.LastIndexOf('.') == -1) {
filename += FileConst.EXT_TEXTURE;
}
// if (!filename.EndsWith(FileConst.EXT_TEXTURE, StringComparison.OrdinalIgnoreCase)) {
// ファイルが存在する場合にのみ適用
if (_fileUtil.Exists(filename)) {
maid.body0.ChangeTex(slotName, matNo, texInfo.propName, filename, null);
// ChangeTexは、nameにファイル名が設定されてしまうため、拡張子を除いた名前を再設定
var changedTex = m.GetTexture(texInfo.propName);
if (changedTex != null) {
changedTex.name = texInfo.texFile;
}
} else {
LogUtil.Debug("texture file not found. file=", filename);
}
}
if (texInfo.offsetX.HasValue || texInfo.offsetY.HasValue) {
var offset = m.GetTextureOffset(texInfo.propName);
if (texInfo.offsetX.HasValue) {
offset.x = texInfo.offsetX.Value;
}
if (texInfo.offsetY.HasValue) {
offset.y = texInfo.offsetY.Value;
}
m.SetTextureOffset(texInfo.propName, offset);
}
if (texInfo.scaleX.HasValue || texInfo.scaleY.HasValue) {
var scale = m.GetTextureScale(texInfo.propName);
if (texInfo.scaleX.HasValue) {
scale.x = texInfo.scaleX.Value;
}
if (texInfo.scaleY.HasValue) {
scale.y = texInfo.scaleY.Value;
}
m.SetTextureScale(texInfo.propName, scale);
}
// フィルタ適用
if (texInfo.filter == null) continue;
var fp = texInfo.filter.ToFilter();
_texModifier.ApplyFilter(maid, slotName, m, texInfo.propName, fp);
}
} else {
LogUtil.LogF("ACCPresetに指定されたマテリアル番号に対応するマテリアルが見つかりません。スキップします。 slot={0}, matNo={1}, name={2}",
ccslot.id, matNo, cmat.name);
break;
}
}
}
}
// disable once MemberCanBeMadeStatic.Local
public void ApplyPresetPartsColor(Maid maid, PresetData preset) {
foreach (var pc in preset.partsColors) {
try {
MaidParts.PARTS_COLOR partsColor;
if (EnumUtil.TryParse(pc.Key, true, out partsColor)) {
maid.Parts.SetPartsColor(partsColor, pc.Value.ToStruct());
}
} catch (ArgumentException e) {
LogUtil.Debug(e);
}
}
}
#if COM3D2
private static readonly Action<Maid, MPN, string, int> SetProp = (maid, mpn, str, id) => {
maid.SetProp(mpn, str, id);
};
#else
private static Action<Maid, MPN, string, int> SetProp;
static PresetManager() {
var typeObj = typeof(Maid);
// 1.56以降
var method = typeObj.GetMethod("SetProp", new[] { typeof(MPN), typeof(string), typeof(int), typeof(bool), typeof(bool) });
if (method != null) {
SetProp = (maid, mpn, str, id) => {
method.Invoke(maid, new object[] { mpn, str, id, false, false });
};
return;
}
// 1.xx ~ 1.55.1
method = typeObj.GetMethod("SetProp", new[] { typeof(MPN), typeof(string), typeof(int), typeof(bool), });
if (method != null) {
SetProp = (maid, mpn, str, id) => {
method.Invoke(maid, new object[] { mpn, str, id, false, });
};
return;
}
// 1.xx 以前
method = typeObj.GetMethod("SetProp", new[] { typeof(MPN), typeof(string), typeof(int), });
if (method != null) {
SetProp = (maid, mpn, str, id) => {
method.Invoke(maid, new object[] { mpn, str, id, });
};
return;
}
SetProp = (maid, mpn, str, id) => {
LogUtil.Log("failed to apply preset(SetProp method) mpn=", mpn);
};
LogUtil.Error("failed to load Maid#SetProp method. Preset-feature dose not work properly.");
}
#endif
}
}