-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomBackgrounds.cs
186 lines (179 loc) · 5.64 KB
/
CustomBackgrounds.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using BepInEx;
using HarmonyLib;
using RenpyLauncher;
using UnityEngine;
using UnityEngine.UI;
namespace DDLC_Custom_Backgrounds
{
[BepInPlugin("com.wackery.custombackground", "Custom Backgrounds DDLC", "1.0.0.0")]
public class CustomBackgrounds : BaseUnityPlugin
{
public static List<CustomBackground> customBackgrounds;
public static int selectedBackground = -1;
public static string GalleryFolder;
public static string SaveFile;
void Awake()
{
GalleryFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Gallery");
SaveFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "gallerysave.txt");
if (!Directory.Exists(GalleryFolder))
{
Directory.CreateDirectory(GalleryFolder);
}
LoadAllBackgrounds();
if (File.Exists(SaveFile))
{
Load(false);
}
else
{
Save();
}
var harmony = new Harmony("com.wackery.custombackgroundpatch");
harmony.PatchAll();
}
public static void Save()
{
if (selectedBackground == -1)
{
File.WriteAllText(SaveFile, "0");
}
else
{
File.WriteAllText(SaveFile, "1" + customBackgrounds[selectedBackground].filename);
}
}
public static void Load(bool refreshBackground)
{
var data = File.ReadAllText(SaveFile);
if (!data.StartsWith("1"))
{
return;
}
data = data.Substring(1);
int i = 0;
foreach (CustomBackground background in customBackgrounds)
{
if (background.filename == data)
{
selectedBackground = i;
}
i++;
}
if (refreshBackground)
{
ActivateBackground();
}
}
void Update()
{
if (!Renpy.IsInitialized())
{
return;
}
if (Renpy.LauncherMain.ActiveAppId != LauncherAppId.Desktop)
{
return;
}
if (Input.GetKeyDown("left"))
{
if (selectedBackground == -1)
{
selectedBackground = customBackgrounds.Count-1;
}
else
{
selectedBackground -= 1;
}
ActivateBackground();
}
else if (Input.GetKeyDown("right"))
{
if (selectedBackground == customBackgrounds.Count-1)
{
selectedBackground = -1;
}
else
{
selectedBackground += 1;
}
ActivateBackground();
}
}
public static void LoadBackground(string filename)
{
CustomBackground background = new CustomBackground();
background.filename = filename;
background.fullfilename = Path.Combine(GalleryFolder,filename);
Texture2D tex = new Texture2D(2, 2);
if (File.Exists(background.fullfilename))
{
var FileData = File.ReadAllBytes(background.fullfilename);
if (tex.LoadImage(FileData)) {
background.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0), 100f);
customBackgrounds.Add(background);
}
}
}
internal void LoadAllBackgrounds()
{
customBackgrounds = new List<CustomBackground>();
var backgrounds = Directory.GetFiles(GalleryFolder);
foreach (string background in backgrounds)
{
Logger.LogInfo($"Loading Background {Path.GetFileName(background)}");
LoadBackground(Path.GetFileName(background));
Logger.LogInfo("Complete!");
}
}
private static void setBackground(CustomBackground background)
{
GameObject.Find("CustomWallpaper").GetComponent<Image>().sprite = background.sprite;
}
internal static void ActivateBackground()
{
setBackground(selectedBackground);
}
public static void setBackground(int backgroundID)
{
selectedBackground = backgroundID;
if (backgroundID == -1) {
Renpy.LauncherMain.CheckForWallpaperChange();
}
else {
setBackground(customBackgrounds[selectedBackground]);
Save();
}
}
}
[HarmonyPatch(typeof(GalleryApp))]
[HarmonyPatch("ConfirmWallpaper")]
class CustomWallpaperPatch
{
public static void Postfix()
{
if (CustomBackgrounds.selectedBackground == -1)
{
return;
}
CustomBackgrounds.setBackground(-1);
}
}
[HarmonyPatch(typeof(DesktopApp))]
[HarmonyPatch("OnAppUpdate")]
class CustomWallpaperPatchAlive
{
static void Postfix()
{
if (CustomBackgrounds.selectedBackground == -1)
{
return;
}
CustomBackgrounds.ActivateBackground();
}
}
}