-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApiController.cs
More file actions
104 lines (97 loc) · 3.05 KB
/
ApiController.cs
File metadata and controls
104 lines (97 loc) · 3.05 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using UnityEngine;
using MelonLoader;
using System.Web.Script.Serialization;
using System.Collections;
using System.Net;
namespace AuthorableModifiers
{
public static class ApiController
{
private static float brightness = RenderSettings.skybox.GetFloat("_Exposure") * 255f;
private static float red, green, blue = 255f;
private static string eventName = "";
public static bool isRunning = false;
private static object token = null;
public static async void PostAsync(float _red, float _green, float _blue, float _brightness, string _eventName)
{
string json = $"{{\"red\": {_red}, \"green\": {_green}, \"blue\": {_blue}, \"bright\": {_brightness}, \"event\": \"{_eventName}\"}}";
try
{
HttpClient client = new HttpClient();
await client.PostAsync(
Config.apiUrl,
new StringContent(json, Encoding.UTF8, "application/json"));
}
catch(HttpRequestException ex)
{
MelonLogger.Msg(ex.InnerException.Message);
}
}
public static IEnumerator StartPost()
{
while (isRunning)
{
if(InGameUI.I.mState != InGameUI.State.PausePage)
{
PostAsync(red, green, blue, brightness, eventName);
}
yield return new WaitForSecondsRealtime(.04f);
}
}
private static void Start()
{
isRunning = true;
brightness = RenderSettings.skybox.GetFloat("_Exposure") * 255f;
eventName = "";
token = MelonCoroutines.Start(StartPost());
}
public static void SetBrightness(float _brightness)
{
if (!Config.postToApi) return;
brightness = _brightness * 255f;
if (!isRunning)
{
Start();
}
}
public static void SetColor(Color color)
{
if (!Config.postToApi) return;
red = color.r * 255f;
green = color.g * 255f;
blue = color.b * 255f;
if (!isRunning)
{
Start();
}
}
public static void SetEvent(string _event)
{
if (!Config.postToApi) return;
eventName = _event;
if (!isRunning)
{
Start();
}
}
public static void TurnOff()
{
if (!Config.postToApi) return;
brightness = 0f;
red = 255f;
green = 255f;
blue = 255;
red = green = blue = 255f;
eventName = "";
isRunning = false;
MelonCoroutines.Stop(token);
PostAsync(red, green, blue, brightness, eventName);
}
}
}