Skip to content

Commit 9f2c5a4

Browse files
committed
Expose additional parsing options in C API
This now allows you to specify the file path and whether you want to load external files via the C API.
1 parent 625d721 commit 9f2c5a4

File tree

10 files changed

+65
-36
lines changed

10 files changed

+65
-36
lines changed

capi/bind_gen/src/csharp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,15 @@ namespace LiveSplitCore
346346
writer,
347347
"{}",
348348
r#"
349-
public static ParseRunResult Parse(Stream stream)
349+
public static ParseRunResult Parse(Stream stream, string path, bool loadFiles)
350350
{
351351
var data = new byte[stream.Length];
352352
stream.Read(data, 0, data.Length);
353353
IntPtr pnt = Marshal.AllocHGlobal(data.Length);
354354
try
355355
{
356356
Marshal.Copy(data, 0, pnt, data.Length);
357-
return Parse(pnt, data.Length);
357+
return Parse(pnt, data.Length, path, loadFiles);
358358
}
359359
finally
360360
{

capi/bind_gen/src/emscripten.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -542,22 +542,22 @@ export "#.to_string()
542542
writer,
543543
"{}",
544544
r#"
545-
static parseArray(data: Int8Array): ParseRunResult {
545+
static parseArray(data: Int8Array, path: string, loadFiles: boolean): ParseRunResult {
546546
const buf = emscriptenModule._malloc(data.length);
547547
try {
548548
emscriptenModule.writeArrayToMemory(data, buf);
549-
const ptr = liveSplitCoreNative.Run_parse(buf, data.length);
549+
const ptr = liveSplitCoreNative.Run_parse(buf, data.length, path, loadFiles ? 1 : 0);
550550
return new ParseRunResult(ptr);
551551
} finally {
552552
emscriptenModule._free(buf);
553553
}
554554
}
555-
static parseString(text: string): ParseRunResult {
555+
static parseString(text: string, path: string, loadFiles: boolean): ParseRunResult {
556556
const len = (text.length << 2) + 1;
557557
const buf = emscriptenModule._malloc(len);
558558
try {
559559
const actualLen = emscriptenModule.stringToUTF8(text, buf, len);
560-
const ptr = liveSplitCoreNative.Run_parse(buf, actualLen);
560+
const ptr = liveSplitCoreNative.Run_parse(buf, actualLen, path, loadFiles ? 1 : 0);
561561
return new ParseRunResult(ptr);
562562
} finally {
563563
emscriptenModule._free(buf);
@@ -571,28 +571,32 @@ export "#.to_string()
571571
r#"
572572
/**
573573
* @param {Int8Array} data
574+
* @param {string} path
575+
* @param {boolean} loadFiles
574576
* @return {ParseRunResult}
575577
*/
576-
static parseArray(data) {
578+
static parseArray(data, path, loadFiles) {
577579
const buf = emscriptenModule._malloc(data.length);
578580
try {
579581
emscriptenModule.writeArrayToMemory(data, buf);
580-
const ptr = liveSplitCoreNative.Run_parse(buf, data.length);
582+
const ptr = liveSplitCoreNative.Run_parse(buf, data.length, path, loadFiles ? 1 : 0);
581583
return new ParseRunResult(ptr);
582584
} finally {
583585
emscriptenModule._free(buf);
584586
}
585587
}
586588
/**
587589
* @param {string} text
590+
* @param {string} path
591+
* @param {boolean} loadFiles
588592
* @return {ParseRunResult}
589593
*/
590-
static parseString(text) {
594+
static parseString(text, path, loadFiles) {
591595
const len = (text.length << 2) + 1;
592596
const buf = emscriptenModule._malloc(len);
593597
try {
594598
const actualLen = emscriptenModule.stringToUTF8(text, buf, len);
595-
const ptr = liveSplitCoreNative.Run_parse(buf, actualLen);
599+
const ptr = liveSplitCoreNative.Run_parse(buf, actualLen, path, loadFiles ? 1 : 0);
596600
return new ParseRunResult(ptr);
597601
} finally {
598602
emscriptenModule._free(buf);

capi/bind_gen/src/java/jna.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public class {class} extends {base_class} implements AutoCloseable {{
349349
writer,
350350
"{}",
351351
r#"
352-
public static ParseRunResult parse(java.io.InputStream stream) throws java.io.IOException {
352+
public static ParseRunResult parse(java.io.InputStream stream, String path, boolean loadFiles) throws java.io.IOException {
353353
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
354354
byte[] buffer = new byte[1024];
355355
while (true) {
@@ -360,7 +360,7 @@ public class {class} extends {base_class} implements AutoCloseable {{
360360
byte[] arr = out.toByteArray();
361361
java.nio.ByteBuffer nativeBuf = java.nio.ByteBuffer.allocateDirect(arr.length);
362362
nativeBuf.put(arr);
363-
return Run.parse(Native.getDirectBufferPointer(nativeBuf), arr.length);
363+
return Run.parse(Native.getDirectBufferPointer(nativeBuf), arr.length, path, loadFiles);
364364
}"#
365365
)?;
366366
}

capi/bind_gen/src/java/jni.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ public class {class} extends {base_class} implements AutoCloseable {{
325325
writer,
326326
"{}",
327327
r#"
328-
public static ParseRunResult parse(String data) {
329-
ParseRunResult result = new ParseRunResult(LiveSplitCoreNative.Run_parseString(data));
328+
public static ParseRunResult parse(String data, String path, boolean loadFiles) {
329+
ParseRunResult result = new ParseRunResult(LiveSplitCoreNative.Run_parseString(data, path, loadFiles));
330330
return result;
331331
}"#
332332
)?;

capi/bind_gen/src/jni_cpp.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ pub fn write<W: Write>(mut writer: W, classes: &BTreeMap<String, Class>) -> Resu
177177
178178
using namespace LiveSplit;
179179
180-
extern "C" JNIEXPORT jlong Java_livesplitcore_LiveSplitCoreNative_Run_1parseString(JNIEnv* jni_env, jobject, jstring data) {
180+
extern "C" JNIEXPORT jlong Java_livesplitcore_LiveSplitCoreNative_Run_1parseString(JNIEnv* jni_env, jobject, jstring data, jstring path, jboolean load_files) {
181181
auto cstr_data = jni_env->GetStringUTFChars(data, nullptr);
182-
auto result = (jlong)Run_parse(cstr_data, strlen(cstr_data));
182+
auto cstr_path = jni_env->GetStringUTFChars(path, nullptr);
183+
auto result = (jlong)Run_parse(cstr_data, strlen(cstr_data), cstr_path, (uint8_t)load_files);
184+
jni_env->ReleaseStringUTFChars(path, cstr_path);
183185
jni_env->ReleaseStringUTFChars(data, cstr_data);
184186
return result;
185187
}

capi/bind_gen/src/kotlin/jni.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ open class {class} : {base_class}, AutoCloseable {{
338338
writer,
339339
"{}",
340340
r#"
341-
fun parse(data: String): ParseRunResult {
342-
val result = ParseRunResult(LiveSplitCoreNative.Run_parseString(data))
341+
fun parse(data: String, path: String, loadFiles: Boolean): ParseRunResult {
342+
val result = ParseRunResult(LiveSplitCoreNative.Run_parseString(data, path, loadFiles))
343343
return result
344344
}"#
345345
)?;

capi/bind_gen/src/node.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -554,20 +554,20 @@ export "#.to_string()
554554
writer,
555555
"{}",
556556
r#"
557-
static parseArray(data: Int8Array): ParseRunResult {
557+
static parseArray(data: Int8Array, path: string, loadFiles: boolean): ParseRunResult {
558558
let buf = Buffer.from(data.buffer);
559559
if (data.byteLength !== data.buffer.byteLength) {
560560
buf = buf.slice(data.byteOffset, data.byteOffset + data.byteLength);
561561
}
562-
return Run.parse(buf, buf.byteLength);
562+
return Run.parse(buf, buf.byteLength, path, loadFiles);
563563
}
564-
static parseFile(file: any): ParseRunResult {
564+
static parseFile(file: any, path: string, loadFiles: boolean): ParseRunResult {
565565
const data = fs.readFileSync(file);
566-
return Run.parse(data, data.byteLength);
566+
return Run.parse(data, data.byteLength, path, loadFiles);
567567
}
568-
static parseString(text: string): ParseRunResult {
568+
static parseString(text: string, path: string, loadFiles: boolean): ParseRunResult {
569569
const data = new Buffer(text);
570-
return Run.parse(data, data.byteLength);
570+
return Run.parse(data, data.byteLength, path, loadFiles);
571571
}"#
572572
)?;
573573
} else {
@@ -577,30 +577,36 @@ export "#.to_string()
577577
r#"
578578
/**
579579
* @param {Int8Array} data
580+
* @param {string} path
581+
* @param {boolean} loadFiles
580582
* @return {ParseRunResult}
581583
*/
582-
static parseArray(data) {
584+
static parseArray(data, path, loadFiles) {
583585
let buf = Buffer.from(data.buffer);
584586
if (data.byteLength !== data.buffer.byteLength) {
585587
buf = buf.slice(data.byteOffset, data.byteOffset + data.byteLength);
586588
}
587-
return Run.parse(buf, buf.byteLength);
589+
return Run.parse(buf, buf.byteLength, path, loadFiles);
588590
}
589591
/**
590592
* @param {string | Buffer | number} file
593+
* @param {string} path
594+
* @param {boolean} loadFiles
591595
* @return {ParseRunResult}
592596
*/
593-
static parseFile(file) {
597+
static parseFile(file, path, loadFiles) {
594598
const data = fs.readFileSync(file);
595-
return Run.parse(data, data.byteLength);
599+
return Run.parse(data, data.byteLength, path, loadFiles);
596600
}
597601
/**
598602
* @param {string} text
603+
* @param {string} path
604+
* @param {boolean} loadFiles
599605
* @return {ParseRunResult}
600606
*/
601-
static parseString(text) {
607+
static parseString(text, path, loadFiles) {
602608
const data = new Buffer(text);
603-
return Run.parse(data, data.byteLength);
609+
return Run.parse(data, data.byteLength, path, loadFiles);
604610
}"#
605611
)?;
606612
}

capi/bind_gen/src/python.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,15 @@ class {class}({base_class}):
288288
writer,
289289
r#"
290290
@staticmethod
291-
def parse_file(file):
291+
def parse_file(file, path, load_files):
292292
data = file.read()
293293
if sys.version_info[0] > 2:
294294
if isinstance(data, str):
295295
raise TypeError("File must be opened in binary mode!")
296296
bytes = bytearray(data)
297297
bufferType = c_byte * len(bytes)
298298
buffer = bufferType(*bytes)
299-
return Run.parse(buffer, len(bytes))"#
299+
return Run.parse(buffer, len(bytes), path, load_files)"#
300300
)?;
301301
}
302302

capi/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ where
124124
}
125125

126126
unsafe fn str(s: *const c_char) -> &'static str {
127-
CStr::from_ptr(s as _).to_str().unwrap()
127+
if s.is_null() {
128+
""
129+
} else {
130+
CStr::from_ptr(s as _).to_str().unwrap()
131+
}
128132
}
129133

130134
fn alloc<T>(data: T) -> *mut T {

capi/src/run.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use livesplit_core::run::{parser, saver};
33
use super::{acc, acc_mut, alloc, output_str, output_time_span, output_vec, own, own_drop, str};
44
use std::io::Cursor;
55
use std::slice;
6+
use std::path::PathBuf;
67
use libc::c_char;
78
use segment::OwnedSegment;
89
use parse_run_result::OwnedParseRunResult;
@@ -21,11 +22,23 @@ pub unsafe extern "C" fn Run_drop(this: OwnedRun) {
2122
}
2223

2324
#[no_mangle]
24-
pub unsafe extern "C" fn Run_parse(data: *const u8, length: usize) -> OwnedParseRunResult {
25+
pub unsafe extern "C" fn Run_parse(
26+
data: *const u8,
27+
length: usize,
28+
path: *const c_char,
29+
load_files: bool,
30+
) -> OwnedParseRunResult {
31+
let path = str(path);
32+
let path = if !path.is_empty() {
33+
Some(PathBuf::from(path))
34+
} else {
35+
None
36+
};
37+
2538
alloc(parser::composite::parse(
2639
Cursor::new(slice::from_raw_parts(data, length)),
27-
None,
28-
false,
40+
path,
41+
load_files,
2942
))
3043
}
3144

0 commit comments

Comments
 (0)