-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
171 lines (147 loc) · 7.54 KB
/
Utils.cs
File metadata and controls
171 lines (147 loc) · 7.54 KB
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
using Frosty.Core;
using FrostySdk.Attributes;
using FrostySdk.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace MeshTextureDuplicationPlugin
{
[DisplayName("Mesh Duplication Options")]
public class MeshDuplicationOptions : OptionsExtension
{
[Category("Meshes")]
[DisplayName("Duplicate Mesh Textures")]
[EbxFieldMeta(EbxFieldType.Boolean)]
[Description("If true, show a popup after duplicating a mesh which facilitates duplicating textures.")]
public bool DupeMeshTextures { get; set; } = true;
[Category("Meshes")]
[DisplayName("Default Naming Pattern")]
[EbxFieldMeta(EbxFieldType.String)]
[DependsOn("DupeMeshTextures")]
[Description("This string decides how the duped textures are named. Use `/` for subfolders. The strings `{TextureName}`, `{ParamName}`, `{Index}`, and `{Suffix}`, if present, will be replaced with the original name of the texture, the `Name` of the texture parameter referencing the texture, the index in the popup, and the guessed suffix of the texture name, respectfully. \n\nI *highly* recommend including `{Index}` to prevent naming conflicts. You must use at least one placeholder. \n\nThis value can also be edited on the fly in the popup.")]
public string DefaultNamingPattern { get; set; } = "Texture/T_{MatId}{Suffix}__{Index}";
[Category("Meshes")]
[DisplayName("Show Warning Popup")]
[EbxFieldMeta(EbxFieldType.Boolean)]
[DependsOn("DupeMeshTextures")]
[Description("If true, show a popup warning users to finish duplicating textures before doing other work.")]
public bool ShowWarningPopup { get; set; } = true;
public override void Load()
{
DupeMeshTextures = Config.Get<bool>("DuplicationPlugin_DupeMeshTextures", true);
DefaultNamingPattern = Config.Get<string>("DuplicationPlugin_DefaultNamingPattern", "Texture/T_{MatId}{Suffix}__{Index}");
ShowWarningPopup = Config.Get<bool>("DuplicationPlugin_ShowWarningPopup", true);
}
public override void Save()
{
Config.Add("DuplicationPlugin_DupeMeshTextures", DupeMeshTextures);
Config.Add("DuplicationPlugin_DefaultNamingPattern", DefaultNamingPattern);
Config.Add("DuplicationPlugin_ShowWarningPopup", ShowWarningPopup);
Config.Save();
}
public override bool Validate()
{
return DefaultNamingPattern.Contains("{TextureName}") || DefaultNamingPattern.Contains("{ParamName}") || DefaultNamingPattern.Contains("{Index}") || DefaultNamingPattern.Contains("{Suffix}") || DefaultNamingPattern.Contains("{MatId}");
}
}
#region Debouncing and Throttling Dispatcher Events - Rick Strahl
// https://weblog.west-wind.com/posts/2017/jul/02/debouncing-and-throttling-dispatcher-events
/// <summary>
/// Provides Debounce() and Throttle() methods.
/// Use these methods to ensure that events aren't handled too frequently.
///
/// Throttle() ensures that events are throttled by the interval specified.
/// Only the last event in the interval sequence of events fires.
///
/// Debounce() fires an event only after the specified interval has passed
/// in which no other pending event has fired. Only the last event in the
/// sequence is fired.
/// </summary>
public class DebounceDispatcher
{
private DispatcherTimer timer;
private DateTime timerStarted { get; set; } = DateTime.UtcNow.AddYears(-1);
/// <summary>
/// Debounce an event by resetting the event timeout every time the event is
/// fired. The behavior is that the Action passed is fired only after events
/// stop firing for the given timeout period.
///
/// Use Debounce when you want events to fire only after events stop firing
/// after the given interval timeout period.
///
/// Wrap the logic you would normally use in your event code into
/// the Action you pass to this method to debounce the event.
/// Example: https://gist.github.com/RickStrahl/0519b678f3294e27891f4d4f0608519a
/// </summary>
/// <param name="interval">Timeout in Milliseconds</param>
/// <param name="action">Action<object> to fire when debounced event fires</object></param>
/// <param name="param">optional parameter</param>
/// <param name="priority">optional priorty for the dispatcher</param>
/// <param name="disp">optional dispatcher. If not passed or null CurrentDispatcher is used.</param>
public void Debounce(int interval, Action<object> action,
object param = null,
DispatcherPriority priority = DispatcherPriority.ApplicationIdle,
Dispatcher disp = null)
{
// kill pending timer and pending ticks
timer?.Stop();
timer = null;
if (disp == null)
disp = Dispatcher.CurrentDispatcher;
// timer is recreated for each event and effectively
// resets the timeout. Action only fires after timeout has fully
// elapsed without other events firing in between
timer = new DispatcherTimer(TimeSpan.FromMilliseconds(interval), priority, (s, e) =>
{
if (timer == null)
return;
timer?.Stop();
timer = null;
action.Invoke(param);
}, disp);
timer.Start();
}
/// <summary>
/// This method throttles events by allowing only 1 event to fire for the given
/// timeout period. Only the last event fired is handled - all others are ignored.
/// Throttle will fire events every timeout ms even if additional events are pending.
///
/// Use Throttle where you need to ensure that events fire at given intervals.
/// </summary>
/// <param name="interval">Timeout in Milliseconds</param>
/// <param name="action">Action<object> to fire when debounced event fires</object></param>
/// <param name="param">optional parameter</param>
/// <param name="priority">optional priorty for the dispatcher</param>
/// <param name="disp">optional dispatcher. If not passed or null CurrentDispatcher is used.</param>
public void Throttle(int interval, Action<object> action,
object param = null,
DispatcherPriority priority = DispatcherPriority.ApplicationIdle,
Dispatcher disp = null)
{
// kill pending timer and pending ticks
timer?.Stop();
timer = null;
if (disp == null)
disp = Dispatcher.CurrentDispatcher;
var curTime = DateTime.UtcNow;
// if timeout is not up yet - adjust timeout to fire
// with potentially new Action parameters
if (curTime.Subtract(timerStarted).TotalMilliseconds < interval)
interval -= (int)curTime.Subtract(timerStarted).TotalMilliseconds;
timer = new DispatcherTimer(TimeSpan.FromMilliseconds(interval), priority, (s, e) =>
{
if (timer == null)
return;
timer?.Stop();
timer = null;
action.Invoke(param);
}, disp);
timer.Start();
timerStarted = curTime;
}
}
#endregion
}