Skip to content

Commit d982889

Browse files
committed
commit
1 parent 807b7a2 commit d982889

File tree

8 files changed

+140
-59
lines changed

8 files changed

+140
-59
lines changed

config.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
{
2+
"check_rate_ms": 1,
23
"language": "en",
3-
"use_osc_query": true,
4-
"sender_ip": "127.0.0.1",
5-
"sender_port": 9000,
64
"receiver_ip": "127.0.0.1",
75
"receiver_port": 9001,
8-
"show_debug_log": false,
9-
"send_all_value_every_time": false,
10-
"check_rate_ms": 1,
116
"restrict_send_rate": true,
12-
"update_handle_addresses": [
13-
"/avatar/parameters/osc_clock@ForceSync"
14-
],
15-
"config_status": "Fallback"
7+
"send_all_value_every_time": false,
8+
"sender_ip": "127.0.0.1",
9+
"sender_port": 9000,
10+
"show_debug_log": false,
11+
"use_osc_query": false
1612
}

orders/orders_osc-clock.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,9 @@
6666
{
6767
"type": "year_3",
6868
"address": "/avatar/parameters/osc_clock@year_3"
69+
},
70+
{
71+
"type": "update_handler",
72+
"address": "/avatar/parameters/osc_clock@ForceSync"
6973
}
7074
]

src/config.rs

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::fmt::Debug;
2-
use serde::{ Serialize, Deserialize };
1+
use crate::log::print_flush;
2+
use crate::log::{print_log, LogType};
3+
use once_cell::sync::Lazy;
4+
use serde::{Deserialize, Serialize};
35
use serde_json::Value;
6+
use std::fmt::Debug;
47
use std::fs::File;
5-
use std::{ fs };
6-
use std::io::{ self, ErrorKind, Read, Write };
8+
use std::io::{self, ErrorKind, Read, Write};
79
use std::sync::Mutex;
8-
use once_cell::sync::Lazy;
9-
use crate::log::{ print_log, LogType };
10-
use crate::log::print_flush;
10+
use std::{fs, vec};
1111

1212
#[derive(Serialize, Deserialize, Debug, Clone)]
1313
pub struct Config {
@@ -21,7 +21,6 @@ pub struct Config {
2121
pub send_all_value_every_time: bool,
2222
pub check_rate_ms: u64,
2323
pub restrict_send_rate: bool,
24-
pub update_handle_addresses: Vec<String>,
2524
pub config_status: String,
2625
}
2726

@@ -52,13 +51,12 @@ impl Default for Config {
5251
send_all_value_every_time: false,
5352
check_rate_ms: 1,
5453
restrict_send_rate: true,
55-
update_handle_addresses: vec!["/avatar/parameters/osc_clock@ForceSync".to_string()],
5654
config_status: format!("{:?}", ConfigStatus::Fallback),
5755
}
5856
}
5957
}
6058

61-
pub static CONFIG: Lazy<Mutex<Config>> = Lazy::new(|| { Mutex::new(load_config()) });
59+
pub static CONFIG: Lazy<Mutex<Config>> = Lazy::new(|| Mutex::new(load_config()));
6260

6361
pub fn init_config() {
6462
Lazy::force(&CONFIG);
@@ -129,7 +127,7 @@ pub fn read_config_json(json_path: &str, complement: bool) -> Result<Config, io:
129127
fn check_itgr(
130128
partial: &serde_json::Value,
131129
default: &serde_json::Value,
132-
exceptions: &[&str]
130+
exceptions: &[&str],
133131
) -> bool {
134132
let mut count = 0;
135133
if let (Value::Object(partial_map), Value::Object(default_map)) = (partial, default) {
@@ -138,7 +136,10 @@ fn check_itgr(
138136
continue;
139137
}
140138
if !partial_map.contains_key(k) {
141-
print_flush(print_log(format!("Lacked property found: {}", k), LogType::WARN));
139+
print_flush(print_log(
140+
format!("Lacked property found: {}", k),
141+
LogType::WARN,
142+
));
142143
count += 1;
143144
}
144145
}
@@ -167,22 +168,37 @@ pub fn repair_config_json(force: bool) -> Result<bool, io::Error> {
167168
print_flush(print_log(format!("Config file found"), LogType::INFO));
168169
}
169170
Err(_error) => {
170-
print_flush(print_log(format!("Failed to load config file"), LogType::WARN));
171+
print_flush(print_log(
172+
format!("Failed to load config file"),
173+
LogType::WARN,
174+
));
171175
}
172176
}
173177
fs::remove_file(path)?;
174178
}
175179
file = File::create("./config.json")?;
176-
let json = serde_json::to_string_pretty(&config)?;
180+
let json =
181+
serde_json::to_string_pretty(&validate(config, vec!["config_status".to_string()]))?;
177182

178-
file.write_all(json.as_bytes()).expect(
179-
&print_log("Failed to write to file".to_string(), LogType::ERROR)
180-
);
183+
file.write_all(json.as_bytes()).expect(&print_log(
184+
"Failed to write to file".to_string(),
185+
LogType::ERROR,
186+
));
181187
}
182188

183189
Ok(true)
184190
}
185191

192+
fn validate(config: Config, exclusions: Vec<String>) -> serde_json::Value {
193+
let mut value = serde_json::to_value(config).unwrap();
194+
if let serde_json::Value::Object(ref mut map) = value {
195+
for key in exclusions {
196+
map.remove(&key);
197+
}
198+
}
199+
value
200+
}
201+
186202
fn load_config() -> Config {
187203
let config;
188204
match read_config_json("./config.json", true) {
@@ -216,34 +232,58 @@ fn load_config() -> Config {
216232
print_flush(print_log("Config file created".to_string(), LogType::INFO));
217233
} else {
218234
config = get_fallback_config();
219-
print_flush(print_log("Using fallback config".to_string(), LogType::WARN));
235+
print_flush(print_log(
236+
"Using fallback config".to_string(),
237+
LogType::WARN,
238+
));
220239
}
221240
}
222241
_ => {
223-
print_flush(print_log(t!("failed_to_load_config").to_string(), LogType::WARN));
224-
print_flush(print_log(t!("how_to_repair_config").to_string(), LogType::INFO));
242+
print_flush(print_log(
243+
t!("failed_to_load_config").to_string(),
244+
LogType::WARN,
245+
));
246+
print_flush(print_log(
247+
t!("how_to_repair_config").to_string(),
248+
LogType::INFO,
249+
));
225250
config = get_fallback_config();
226251
}
227252
}
228253
}
229254
}
230255

231256
if config.send_all_value_every_time {
232-
print_flush(print_log(t!("warning_send_all_value").to_string(), LogType::WARN));
257+
print_flush(print_log(
258+
t!("warning_send_all_value").to_string(),
259+
LogType::WARN,
260+
));
233261
}
234262

235263
if !config.restrict_send_rate {
236-
print_flush(print_log(t!("warning_restrict_send_rate").to_string(), LogType::WARN));
264+
print_flush(print_log(
265+
t!("warning_restrict_send_rate").to_string(),
266+
LogType::WARN,
267+
));
237268
}
238269

239270
if config.check_rate_ms == 0 {
240-
print_flush(print_log(t!("warning_check_rate_ms_zero").to_string(), LogType::WARN));
271+
print_flush(print_log(
272+
t!("warning_check_rate_ms_zero").to_string(),
273+
LogType::WARN,
274+
));
241275
} else if config.check_rate_ms > 100 {
242-
print_flush(print_log(t!("warning_check_rate_ms_too_much").to_string(), LogType::WARN));
276+
print_flush(print_log(
277+
t!("warning_check_rate_ms_too_much").to_string(),
278+
LogType::WARN,
279+
));
243280
}
244281

245282
if config.use_osc_query {
246-
print_flush(print_log(t!("warning_osc_query_enabled").to_string(), LogType::INFO));
283+
print_flush(print_log(
284+
t!("warning_osc_query_enabled").to_string(),
285+
LogType::INFO,
286+
));
247287
}
248288

249289
return config;

src/order.rs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1+
use crate::log::{print_flush, print_log, LogType};
2+
use crate::unit::UnitType;
13
use once_cell::sync::Lazy;
2-
use serde::{ Deserialize, Serialize };
4+
use serde::{Deserialize, Serialize};
5+
use serde_json;
36
use std::io::Write;
4-
use std::{fs, io};
57
use std::path::Path;
6-
use serde_json;
7-
use crate::log::{print_flush, print_log, LogType};
8-
use crate::unit::UnitType;
8+
use std::{fs, io};
99

1010
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1111
pub struct Order {
1212
pub r#type: UnitType,
1313
pub address: String,
1414
}
1515

16-
pub static ORDERS: Lazy<Vec<Order>> = Lazy::new(|| load_orders());
16+
#[derive(Debug, Clone, PartialEq, Eq)]
17+
pub struct Orders {
18+
pub sender: Vec<Order>,
19+
pub handler: Vec<Order>,
20+
}
21+
22+
pub static ORDERS: Lazy<Orders> = Lazy::new(|| load_orders());
1723

1824
pub fn init_orders() {
1925
Lazy::force(&ORDERS);
2026
}
2127

22-
pub fn load_orders() -> Vec<Order> {
28+
pub fn load_orders() -> Orders {
2329
let mut orders = Vec::new();
2430
let orders_dir = Path::new("orders");
2531

2632
if !orders_dir.exists() {
2733
if let Err(e) = fs::create_dir_all(orders_dir) {
2834
eprintln!("Failed to create orders directory: {}", e);
29-
return orders;
35+
return split(get_fallback_orders());
3036
}
3137
}
3238

@@ -62,13 +68,34 @@ pub fn load_orders() -> Vec<Order> {
6268
let _ = fs::write(&default_path, json);
6369
}
6470
print_flush(print_log("Orders file created".to_string(), LogType::INFO));
65-
orders = load_orders();
71+
return load_orders();
6672
} else {
6773
orders = get_fallback_orders();
68-
print_flush(print_log("Using fallback orders".to_string(), LogType::WARN));
74+
print_flush(print_log(
75+
"Using fallback orders".to_string(),
76+
LogType::WARN,
77+
));
78+
}
79+
}
80+
split(orders)
81+
}
82+
83+
fn split(orders: Vec<Order>) -> Orders {
84+
let mut sender = Vec::new();
85+
let mut handler = Vec::new();
86+
87+
for order in orders {
88+
if order.r#type == UnitType::UpdateHandler {
89+
handler.push(order);
90+
} else {
91+
sender.push(order);
6992
}
7093
}
71-
orders
94+
95+
Orders {
96+
sender,
97+
handler,
98+
}
7299
}
73100

74101
pub fn get_fallback_orders() -> Vec<Order> {
@@ -109,7 +136,10 @@ pub fn get_fallback_orders() -> Vec<Order> {
109136
r#type: UnitType::IsPm,
110137
address: "/avatar/parameters/osc_clock@hour_isPM".to_string(),
111138
},
112-
Order { r#type: UnitType::DayInt, address: "/avatar/parameters/osc_clock@day".to_string() },
139+
Order {
140+
r#type: UnitType::DayInt,
141+
address: "/avatar/parameters/osc_clock@day".to_string(),
142+
},
113143
Order {
114144
r#type: UnitType::DayOfWeekInt,
115145
address: "/avatar/parameters/osc_clock@dofw".to_string(),
@@ -118,7 +148,10 @@ pub fn get_fallback_orders() -> Vec<Order> {
118148
r#type: UnitType::MonthInt,
119149
address: "/avatar/parameters/osc_clock@month".to_string(),
120150
},
121-
Order { r#type: UnitType::Year, address: "/avatar/parameters/osc_clock@year".to_string() },
151+
Order {
152+
r#type: UnitType::Year,
153+
address: "/avatar/parameters/osc_clock@year".to_string(),
154+
},
122155
Order {
123156
r#type: UnitType::Year0,
124157
address: "/avatar/parameters/osc_clock@year_0".to_string(),
@@ -134,6 +167,10 @@ pub fn get_fallback_orders() -> Vec<Order> {
134167
Order {
135168
r#type: UnitType::Year3,
136169
address: "/avatar/parameters/osc_clock@year_3".to_string(),
137-
}
170+
},
171+
Order {
172+
r#type: UnitType::UpdateHandler,
173+
address: "/avatar/parameters/osc_clock@ForceSync".to_string(),
174+
},
138175
]
139176
}

src/osc_query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ pub async fn start() -> Result<(), Error> {
3939
vrchat_osc.register(&service_name, root_node, |packet| {
4040
if let OscPacket::Message(msg) = packet {
4141
let config = CONFIG.lock().unwrap().clone();
42-
if check(msg.clone(), config.clone()) {
42+
if check(msg.clone(), ORDERS.clone().handler) {
4343
let flag = SyncFlag::MINUTE | SyncFlag::HOUR | SyncFlag::DAY;
4444
let messages = build(BuilderParams {
45-
orders: ORDERS.clone(),
45+
orders: ORDERS.clone().sender,
4646
sync_flag: flag,
4747
});
4848
for message in messages {

src/receiver.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use chrono::Local;
22
use rosc::{ OscMessage, OscPacket, OscType };
33
use std::net::{ UdpSocket, SocketAddr };
44

5-
use crate::config::{ Config, CONFIG };
5+
use crate::config::{ CONFIG };
66
use crate::log::{ print_log, print_flush, LogType };
77
use crate::message::{ build, BuilderParams, SyncFlag };
8-
use crate::order::ORDERS;
8+
use crate::order::{Order, ORDERS};
99
use crate::sender::send;
1010

1111
pub async fn receiver() {
@@ -34,10 +34,10 @@ pub async fn receiver() {
3434
match packet {
3535
(_, OscPacket::Message(msg)) => {
3636
config = CONFIG.lock().unwrap().clone();
37-
if check(msg.clone(), config.clone()) {
37+
if check(msg.clone(), ORDERS.clone().handler) {
3838
let flag = SyncFlag::MINUTE | SyncFlag::HOUR | SyncFlag::DAY;
3939
let messages = build(BuilderParams {
40-
orders: ORDERS.clone(),
40+
orders: ORDERS.clone().sender,
4141
sync_flag: flag,
4242
});
4343
for message in messages {
@@ -67,7 +67,7 @@ pub async fn receiver() {
6767
}
6868
}
6969

70-
pub fn check(msg: OscMessage, config: Config) -> bool {
70+
pub fn check(msg: OscMessage, order: Vec<Order>) -> bool {
7171
let update: bool;
7272

7373
match msg.args[0] {
@@ -83,8 +83,8 @@ pub fn check(msg: OscMessage, config: Config) -> bool {
8383
}
8484
}
8585
if update {
86-
for n in 0..config.update_handle_addresses.len() {
87-
if msg.addr.to_string() == config.update_handle_addresses[n].to_string() {
86+
for n in 0..order.len() {
87+
if msg.addr.to_string() == order[n].address.to_string() {
8888
print_flush(
8989
print_log(
9090
t!(

src/sender.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub async fn sender<F, Fut>(s: F)
6565
}
6666
}
6767
let messages = build(BuilderParams {
68-
orders: ORDERS.clone(),
68+
orders: ORDERS.clone().sender,
6969
sync_flag: flag,
7070
});
7171
for message in messages {

0 commit comments

Comments
 (0)