Skip to content

Commit

Permalink
Expands loading scripts to include from a string
Browse files Browse the repository at this point in the history
  • Loading branch information
JujuAdams committed Jan 19, 2021
1 parent 63a54b8 commit d70668e
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 72 deletions.
19 changes: 11 additions & 8 deletions chatterbox.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions objects/obj_test/Create_0.gml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//Load in some source files
chatterbox_load("Test.json");
chatterbox_load("Test2.json");
chatterbox_load("Test2.yarn");
chatterbox_load_from_file("Test.json");
chatterbox_load_from_file("Test2.json");
chatterbox_load_from_file("Test2.yarn");

chatterbox_add_function("TestFunctionDoNotExecute", function(_array) { show_message(_array); });

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/__chatterbox_class_node/__chatterbox_class_node.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions scripts/__chatterbox_class_source/__chatterbox_class_source.gml
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/// @param filename
/// @param buffer
/// @param string

function __chatterbox_class_source(_filename, _buffer) constructor
function __chatterbox_class_source(_filename, _string) constructor
{
filename = _filename;
name = _filename;
format = undefined;
nodes = [];
loaded = false; //We set this to <true> at the bottom of the constructor

//Read a string from the buffer
var _old_tell = buffer_tell(_buffer);
buffer_seek(_buffer, buffer_seek_start, 0);
var _string = buffer_read(_buffer, buffer_string);
buffer_seek(_buffer, buffer_seek_start, _old_tell);

if (os_browser != browser_not_a_browser)
{
__chatterbox_trace("Replacing tabs with spaces to work around GM's janky as f*** JSON parser");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/__chatterbox_evaluate/__chatterbox_evaluate.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/__chatterbox_system/__chatterbox_system.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/__chatterbox_vm/__chatterbox_vm.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 2 additions & 44 deletions scripts/chatterbox_load/chatterbox_load.gml
Original file line number Diff line number Diff line change
@@ -1,48 +1,6 @@
/// Loads a Yarn file (either .yarn or .json) for use with Chatterbox
///
/// To find out more about Chatterbox's scripting language, "Yarn", please read the __chatterbox_syntax()
///
/// @param filename Name of the file to add

function chatterbox_load(_filename)
{
if (!is_string(_filename))
{
__chatterbox_error("Files should be loaded using their filename as a string.\n(Input was an invalid datatype)");
return undefined;
}
__chatterbox_error("chatterbox_load() has been deprecated. Please use chatterbox_load_from_file() instead");

//Fix the font directory name if it's weird
var _font_directory = CHATTERBOX_SOURCE_DIRECTORY;
var _char = string_char_at(_font_directory , string_length(_font_directory ));
if (_char != "\\") && (_char != "/") _font_directory += "\\";

if (!file_exists(_font_directory + _filename))
{
__chatterbox_error("\"", _filename, "\" could not be found");
return undefined;
}

if (os_browser == browser_not_a_browser)
{
var _buffer = buffer_load(_font_directory + _filename);
}
else
{
__chatterbox_trace("Using legacy file loading method on HTML5");

var _file = file_text_open_read(_font_directory + _filename);

var _string = "";
while(!file_text_eof(_file)) _string += file_text_readln(_file);
file_text_close(_file);

var _buffer = buffer_create(string_byte_length(_string), buffer_fixed, 1);
buffer_write(_buffer, buffer_text, _string);
buffer_seek(_buffer, buffer_seek_start, 0);

show_debug_message(_string);
}

return chatterbox_load_from_buffer(_filename, _buffer);
return chatterbox_load_from_file(_filename);
}
4 changes: 2 additions & 2 deletions scripts/chatterbox_load/chatterbox_load.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ function chatterbox_load_from_buffer(_filename, _buffer)
//Set our default file if we don't already have one
if (global.__chatterbox_default_file == "") global.__chatterbox_default_file = _filename;

//Read a string from the buffer
var _old_tell = buffer_tell(_buffer);
buffer_seek(_buffer, buffer_seek_start, 0);
var _string = buffer_read(_buffer, buffer_string);
buffer_seek(_buffer, buffer_seek_start, _old_tell);

//Create a struct that represents this source
var _source = new __chatterbox_class_source(_filename, _buffer);
var _source = new __chatterbox_class_source(_filename, _string);

//If we successfully decoded a buffer add it to our collection of chatterboxes
if ((instanceof(_source) == "__chatterbox_class_source") && !is_undefined(_source.format))
Expand Down
48 changes: 48 additions & 0 deletions scripts/chatterbox_load_from_file/chatterbox_load_from_file.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// Loads a Yarn file (either .yarn or .json) for use with Chatterbox
///
/// To find out more about Chatterbox's scripting language, "Yarn", please read the __chatterbox_syntax()
///
/// @param filename Name of the file to add

function chatterbox_load_from_file(_filename)
{
if (!is_string(_filename))
{
__chatterbox_error("Files should be loaded using their filename as a string.\n(Input was an invalid datatype)");
return undefined;
}

//Fix the font directory name if it's weird
var _font_directory = CHATTERBOX_SOURCE_DIRECTORY;
var _char = string_char_at(_font_directory , string_length(_font_directory ));
if (_char != "\\") && (_char != "/") _font_directory += "\\";

if (!file_exists(_font_directory + _filename))
{
__chatterbox_error("\"", _filename, "\" could not be found");
return undefined;
}

if (os_browser == browser_not_a_browser)
{
var _buffer = buffer_load(_font_directory + _filename);
}
else
{
__chatterbox_trace("Using legacy file loading method on HTML5");

var _file = file_text_open_read(_font_directory + _filename);

var _string = "";
while(!file_text_eof(_file)) _string += file_text_readln(_file);
file_text_close(_file);

var _buffer = buffer_create(string_byte_length(_string), buffer_fixed, 1);
buffer_write(_buffer, buffer_text, _string);
buffer_seek(_buffer, buffer_seek_start, 0);

show_debug_message(_string);
}

return chatterbox_load_from_buffer(_filename, _buffer);
}
12 changes: 12 additions & 0 deletions scripts/chatterbox_load_from_file/chatterbox_load_from_file.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions scripts/chatterbox_load_from_string/chatterbox_load.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions scripts/chatterbox_load_from_string/chatterbox_load_from_buffer.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Loads a Yarn source for use with Chatterbox directly from a string
///
/// To find out more about Chatterbox's scripting language, "Yarn", please read the __chatterbox_syntax()
///
/// @param filename Filename to use for this buffer
/// @param string String to read

function chatterbox_load_from_string(_filename, _string)
{
if (!is_string(_filename))
{
__chatterbox_error("Buffers should have a filename specified as a string.\n(Input was an invalid datatype)");
return undefined;
}

if (chatterbox_is_loaded(_filename))
{
//Unload what we have already if needed
//This will invalidate any chatterboxes that currently exist and are using the file
chatterbox_unload(_filename);
}

//Set our default file if we don't already have one
if (global.__chatterbox_default_file == "") global.__chatterbox_default_file = _filename;

//Create a struct that represents this source
var _source = new __chatterbox_class_source(_filename, _string);

//If we successfully decoded a buffer add it to our collection of chatterboxes
if ((instanceof(_source) == "__chatterbox_class_source") && !is_undefined(_source.format))
{
global.chatterbox_files[? _filename] = _source;
}
}
12 changes: 12 additions & 0 deletions scripts/chatterbox_load_from_string/chatterbox_load_from_string.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d70668e

Please sign in to comment.