Skip to content

Commit 903df3f

Browse files
committed
Added ThemeFileSerializer
1 parent 0b01597 commit 903df3f

5 files changed

Lines changed: 161 additions & 19 deletions

File tree

ThemePacker/MainForm.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Deployment.Compression.Cab;
2+
using Newtonsoft.Json.Linq;
23
using System;
34
using System.Collections.Generic;
45
using System.Data;
@@ -25,9 +26,8 @@ public partial class ThemePacker : Form
2526
private Dictionary<string, Image> _pictures;
2627
private string _path;
2728
private int _currentPic;
28-
private bool _isFolderBased;
2929

30-
private CustomProgressBar pbProgression;
30+
private CustomProgressBar _pbProgression;
3131

3232
private EventHandler _btnNextClick;
3333

@@ -36,14 +36,14 @@ public partial class ThemePacker : Form
3636
public ThemePacker()
3737
{
3838
InitializeComponent();
39-
pbProgression = new CustomProgressBar(); pbProgression.Location = new Point(69, 496);
40-
pbProgression.MarqueeAnimationSpeed = 0;
41-
pbProgression.Name = "pbProgression";
42-
pbProgression.Size = new Size(400, 28);
43-
pbProgression.Style = ProgressBarStyle.Continuous;
44-
pbProgression.TabIndex = 16;
45-
pbProgression.DisplayStyle = ProgressBarDisplayText.CustomText;
46-
Controls.Add(pbProgression);
39+
_pbProgression = new CustomProgressBar(); _pbProgression.Location = new Point(69, 496);
40+
_pbProgression.MarqueeAnimationSpeed = 0;
41+
_pbProgression.Name = "pbProgression";
42+
_pbProgression.Size = new Size(400, 28);
43+
_pbProgression.Style = ProgressBarStyle.Continuous;
44+
_pbProgression.TabIndex = 16;
45+
_pbProgression.DisplayStyle = ProgressBarDisplayText.CustomText;
46+
Controls.Add(_pbProgression);
4747
}
4848

4949
private void ThemePacker_Load(object sender, EventArgs e)
@@ -98,7 +98,6 @@ private void ChooseImageFolderToolStripMenuItem_Click(object sender, EventArgs e
9898
btnNext.Enabled = true;
9999
btnPrevious.Enabled = true;
100100
ListShuffle();
101-
_isFolderBased = true;
102101
UpdatePic();
103102
}
104103
}
@@ -113,8 +112,6 @@ private async void GenerateFormInspirobotToolStripMenuItem_Click(object sender,
113112

114113
_currentPic = 0;
115114

116-
_isFolderBased = false;
117-
118115
_btnNextClick = BtnNext_Click_API;
119116
btnNext.Click += _btnNextClick;
120117

@@ -167,9 +164,9 @@ private void BtnPrevious_Click(object sender, EventArgs e)
167164
private void UpdatePic()
168165
{
169166
pbWallpaper.Image = _pictures.ElementAt(_currentPic).Value;
170-
pbProgression.Value = (int)Math.Ceiling(((_currentPic + 1) * 100) / (decimal)(_pictures.Count));
171-
pbProgression.CustomText = $"{_currentPic + 1} / {_pictures.Count}";
172-
pbProgression.Refresh();
167+
_pbProgression.Value = (int)Math.Ceiling(((_currentPic + 1) * 100) / (decimal)(_pictures.Count));
168+
_pbProgression.CustomText = $"{_currentPic + 1} / {_pictures.Count}";
169+
_pbProgression.Refresh();
173170
}
174171

175172
private void BtnNext_Click(object sender, EventArgs e)
@@ -234,10 +231,19 @@ public void Generate()
234231

235232
CopyQuick();
236233

234+
ThemeFileSerializer tfs = new ThemeFileSerializer("temp\\super.theme");
235+
tfs.Deserialize();
236+
JObject theme = tfs.JSON;
237+
theme["Theme"]["DisplayName"] = fileName;
238+
theme["Control Panel_Desktop"]["TileWallpaper"] = "0";
239+
theme["Control Panel_Desktop"]["WallpaperStyle"] = "2";
240+
theme["Slideshow"]["Interval"] = "60000";
241+
242+
tfs.JSON = theme;
243+
tfs.JsonSerialize();
237244

238245
//read and replace
239246
string text = File.ReadAllText("temp\\super.theme");
240-
text = text.Replace("DisplayName=Tinderspirobot", "DisplayName=" + fileName);
241247
File.WriteAllText("temp\\super.theme", text);
242248

243249
CabInfo cab = new CabInfo(saveFileDialog.FileName);
@@ -246,6 +252,11 @@ public void Generate()
246252
Environment.Exit(7);
247253
}
248254

255+
private void SetThemeFileSerializerProperty(dynamic theme, string category, string property, string value)
256+
{
257+
((theme as IDictionary<string, object>)[category] as IDictionary<string, object>)[property] = value;
258+
}
259+
249260
private void CopyQuick()
250261
{
251262
foreach (string path in _likeds)

ThemePacker/Resources/Super.theme

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ SchemeName=@mmres.dll,-800
6565
[Theme.A]
6666
[Theme.W]
6767
DisplayName=Youpo
68+
6869
[Slideshow]
6970
ImagesRootPath=DesktopBackground
70-
7171
Interval=1000
7272
Shuffle=1
73-
ImagesRootPath=DesktopBackground

ThemePacker/ThemeFileSerializer.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Dynamic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace ThemePacker
12+
{
13+
public class ThemeFileSerializer
14+
{
15+
private string _path;
16+
17+
public JObject JSON { get; set; }
18+
public dynamic Theme { get; set; }
19+
20+
public ThemeFileSerializer(string path)
21+
{
22+
_path = path;
23+
24+
JSON = new JObject();
25+
Theme = new ExpandoObject();
26+
}
27+
28+
public dynamic Deserialize()
29+
{
30+
var themeDic = new Dictionary<string, Dictionary<string, string>>();
31+
32+
using (StreamReader sr = new StreamReader(_path))
33+
{
34+
string line;
35+
string parent = "";
36+
37+
while ((line = sr.ReadLine()) != null)
38+
{
39+
if (!(line.StartsWith(";") || string.IsNullOrEmpty(line)))
40+
{
41+
if (line.StartsWith("["))
42+
{
43+
parent = line.TrimStart('[').TrimEnd(']').Replace('\\', '_');
44+
themeDic.Add(parent, new Dictionary<string, string>());
45+
}
46+
else
47+
{
48+
string[] property = line.Split('=');
49+
string key = property[0];
50+
string value = property.Length == 1 ? "" : property[1];
51+
52+
themeDic[parent].Add(key, value);
53+
}
54+
}
55+
}
56+
}
57+
58+
JSON = JObject.FromObject(themeDic);
59+
60+
Theme = new ExpandoObject();
61+
62+
var expandoDict = Theme as IDictionary<string, object>;
63+
64+
foreach (var category in themeDic)
65+
{
66+
expandoDict.Add(category.Key, new ExpandoObject());
67+
68+
foreach (var prop in category.Value)
69+
{
70+
(expandoDict[category.Key] as IDictionary<string, object>).Add(prop.Key, prop.Value);
71+
}
72+
}
73+
74+
return Theme;
75+
}
76+
77+
public void Serialize(string path = null)
78+
{
79+
if (path == null)
80+
path = _path;
81+
82+
using (StreamWriter sw = new StreamWriter(path))
83+
{
84+
var themeDic = Theme as IDictionary<string, object>;
85+
86+
foreach (var category in themeDic)
87+
{
88+
sw.WriteLine($"[{category.Key.Replace('_', '\\')}]");
89+
90+
foreach (var prop in category.Value as IDictionary<string, object>)
91+
{
92+
sw.WriteLine($"{prop.Key}={prop.Value}");
93+
}
94+
95+
sw.WriteLine();
96+
}
97+
98+
sw.Close();
99+
}
100+
}
101+
102+
public void JsonSerialize(string path = null)
103+
{
104+
if (path == null)
105+
path = _path;
106+
107+
var themeDic = JSON.ToObject<Dictionary<string, Dictionary<string, string>>>();
108+
109+
using (StreamWriter sw = new StreamWriter(path))
110+
{
111+
foreach (var category in themeDic)
112+
{
113+
sw.WriteLine($"[{category.Key.Replace('_', '\\')}]");
114+
115+
foreach (var prop in category.Value)
116+
{
117+
sw.WriteLine($"{prop.Key}={prop.Value}");
118+
}
119+
120+
sw.WriteLine();
121+
}
122+
123+
sw.Close();
124+
}
125+
}
126+
}
127+
}

ThemePacker/ThemePacker.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<Reference Include="Microsoft.Deployment.Compression.Cab, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ce35f76fcda82bad, processorArchitecture=MSIL">
4040
<HintPath>..\packages\MSFTCompressionCab.1.0.0\lib\Microsoft.Deployment.Compression.Cab.dll</HintPath>
4141
</Reference>
42+
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
43+
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
44+
</Reference>
4245
<Reference Include="System" />
4346
<Reference Include="System.Core" />
4447
<Reference Include="System.Xml.Linq" />
@@ -69,6 +72,7 @@
6972
</Compile>
7073
<Compile Include="Program.cs" />
7174
<Compile Include="Properties\AssemblyInfo.cs" />
75+
<Compile Include="ThemeFileSerializer.cs" />
7276
<EmbeddedResource Include="MainForm.resx">
7377
<DependentUpon>MainForm.cs</DependentUpon>
7478
</EmbeddedResource>

ThemePacker/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="MSFTCompressionCab" version="1.0.0" targetFramework="net461" />
4+
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
45
</packages>

0 commit comments

Comments
 (0)