hocclk: optimize kip routines

This commit is contained in:
souldbminersmwc
2026-05-29 18:18:51 -04:00
parent 1d56ed1504
commit a95c0fc23c
5 changed files with 58 additions and 51 deletions

View File

@@ -34,19 +34,17 @@ namespace kip {
// }
// }
CustomizeTable table;
FILE* fp;
fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r");
FILE* fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r+b");
if (fp == NULL) {
notification::writeNotification("Horizon OC\nKip opening failed");
kipAvailable = false;
return;
} else {
kipAvailable = true;
fclose(fp);
}
kipAvailable = true;
if (!cust_read_and_cache("sdmc:/atmosphere/kips/hoc.kip", &table)) {
if (!cust_read_table_f(fp, &table)) {
fclose(fp);
fileUtils::LogLine("[kip] Failed to read KIP file");
notification::writeNotification("Horizon OC\nKip read failed");
return;
@@ -55,11 +53,13 @@ namespace kip {
u32 custRev = cust_get_cust_rev(&table);
u32 kipVersion = cust_get_kip_version(&table);
if (custRev < CUST_REV || kipVersion < KIP_VERSION) {
fclose(fp);
notification::writeNotification("Horizon OC\nOutdated kip detected!\nPlease update Horizon OC");
fileUtils::LogLine("Cust revision: %u", custRev);
fileUtils::LogLine("Kip version: %u", kipVersion);
return;
} else if (custRev > CUST_REV || kipVersion > KIP_VERSION) {
fclose(fp);
notification::writeNotification("Horizon OC\nOutdated sysmodule detected!\nPlease update Horizon OC");
fileUtils::LogLine("Cust revision: %u", custRev);
fileUtils::LogLine("Kip version: %u", kipVersion);
@@ -138,10 +138,13 @@ namespace kip {
CUST_WRITE_FIELD_BATCH(&table, t6_tRTW_fine_tune, config::GetConfigValue(KipConfigValue_t6_tRTW_fine_tune));
CUST_WRITE_FIELD_BATCH(&table, t7_tWTR_fine_tune, config::GetConfigValue(KipConfigValue_t7_tWTR_fine_tune));
if (!cust_write_table("sdmc:/atmosphere/kips/hoc.kip", &table)) {
if (!cust_write_table_f(fp, &table)) {
fclose(fp);
fileUtils::LogLine("[kip] Failed to write KIP file");
notification::writeNotification("Horizon OC\nKip write failed");
return;
}
fclose(fp);
HocClkConfigValueList configValues;
config::GetConfigValues(&configValues);
@@ -163,27 +166,26 @@ namespace kip {
void GetKipData()
{
FILE* fp;
fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r");
FILE* fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "rb");
if (fp == NULL) {
notification::writeNotification("Horizon OC\nKip opening failed");
kipAvailable = false;
return;
} else {
kipAvailable = true;
fclose(fp);
}
kipAvailable = true;
HocClkConfigValueList configValues;
config::GetConfigValues(&configValues);
CustomizeTable table;
if (!cust_read_and_cache("sdmc:/atmosphere/kips/hoc.kip", &table)) {
if (!cust_read_table_f(fp, &table)) {
fclose(fp);
fileUtils::LogLine("[kip] Failed to read KIP file for GetKipData");
notification::writeNotification("Horizon OC\nKip read failed");
return;
}
fclose(fp);
// if(cust_get_cust_rev(&table) != CUST_REV) {
// notification::writeNotification("Horizon OC\nKip version mismatch\nPlease reinstall Horizon OC");

View File

@@ -128,54 +128,59 @@ namespace kip {
return false;
}
static long cachedCustOffset = -1;
static inline long cust_get_offset(FILE* f) {
if (cachedCustOffset >= 0) return cachedCustOffset;
long off;
if (!cust_find_offset(f, &off)) return -1;
cachedCustOffset = off;
return off;
}
static inline bool cust_read_table_f(FILE* f, CustomizeTable* out) {
long off = cust_get_offset(f);
if (off < 0) return false;
fseek(f, 0, SEEK_END);
if (off + (long)sizeof(CustomizeTable) > ftell(f)) return false;
fseek(f, off, SEEK_SET);
if (fread(out, 1, sizeof(CustomizeTable), f) != sizeof(CustomizeTable)) return false;
if (memcmp(out->cust, CUST_MAGIC, CUST_MAGIC_LEN) != 0) {
cachedCustOffset = -1;
return false;
}
return true;
}
static inline bool cust_write_table_f(FILE* f, const CustomizeTable* in) {
long off = cust_get_offset(f);
if (off < 0) return false;
fseek(f, 0, SEEK_END);
if (off + (long)sizeof(CustomizeTable) > ftell(f)) return false;
fseek(f, off, SEEK_SET);
bool ok = fwrite(in, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable);
fflush(f);
return ok;
}
static inline bool cust_read_table(const char* path, CustomizeTable* out) {
FILE* f = fopen(path, "rb");
if (!f) return false;
long off;
if (!cust_find_offset(f, &off)) {
fclose(f);
return false;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
if (off + (long)sizeof(CustomizeTable) > size) {
fclose(f);
return false;
}
fseek(f, off, SEEK_SET);
bool ok = fread(out, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable);
bool ok = cust_read_table_f(f, out);
fclose(f);
return ok && memcmp(out->cust, CUST_MAGIC, CUST_MAGIC_LEN) == 0;
return ok;
}
static inline bool cust_write_table(const char* path, const CustomizeTable* in) {
FILE* f = fopen(path, "r+b");
if (!f) return false;
long off;
if (!cust_find_offset(f, &off)) {
fclose(f);
return false;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
if (off + (long)sizeof(CustomizeTable) > size) {
fclose(f);
return false;
}
fseek(f, off, SEEK_SET);
bool ok = fwrite(in, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable);
fflush(f);
bool ok = cust_write_table_f(f, in);
fclose(f);
return ok;
}

Binary file not shown.

Binary file not shown.