[gnome-games] love: Add LovePackage



commit 6cc79ce58bc83a593c03ff44f37dd8e86180e3b2
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed May 11 18:54:59 2016 +0200

    love: Add LovePackage
    
    This will be used in next commit when adding LoveIcon to avoid code copy
    with LoveGame for code managing the LÖVE package.
    icons.

 plugins/love/src/Makefile.am       |    2 +
 plugins/love/src/love-error.vala   |    5 +
 plugins/love/src/love-game.vala    |   97 ++----------------------
 plugins/love/src/love-package.vala |  146 ++++++++++++++++++++++++++++++++++++
 4 files changed, 160 insertions(+), 90 deletions(-)
---
diff --git a/plugins/love/src/Makefile.am b/plugins/love/src/Makefile.am
index 6dc14c2..dc6a346 100644
--- a/plugins/love/src/Makefile.am
+++ b/plugins/love/src/Makefile.am
@@ -6,7 +6,9 @@ libgames_love_plugin_la_DEPENDENCIES = \
        $(NULL)
 
 libgames_love_plugin_la_SOURCES = \
+       love-error.vala \
        love-game.vala \
+       love-package.vala \
        love-plugin.vala \
        $(NULL)
 
diff --git a/plugins/love/src/love-error.vala b/plugins/love/src/love-error.vala
new file mode 100644
index 0000000..a85f123
--- /dev/null
+++ b/plugins/love/src/love-error.vala
@@ -0,0 +1,5 @@
+// This file is part of GNOME Games. License: GPLv3
+
+private errordomain Games.LoveError {
+       INVALID_PACKAGE,
+}
diff --git a/plugins/love/src/love-game.vala b/plugins/love/src/love-game.vala
index 6b81685..2e51f1d 100644
--- a/plugins/love/src/love-game.vala
+++ b/plugins/love/src/love-game.vala
@@ -1,8 +1,6 @@
 // This file is part of GNOME Games. License: GPLv3
 
 private class Games.LoveGame : Object, Game {
-       private const size_t BLOCK_SIZE = 4096;
-
        private string _name;
        public string name {
                get { return _name; }
@@ -25,13 +23,16 @@ private class Games.LoveGame : Object, Game {
                }
        }
 
-       public LoveGame (string uri) {
+       public LoveGame (string uri) throws Error {
+               var package = new LovePackage (uri);
+
                var file = File.new_for_uri (uri);
                path = file.get_path ();
 
-               var config_file = parse_package (path);
-               if (config_file != null)
-                       parse_config_file (config_file);
+               _name = package.get_config ("title");
+
+               if (name == null)
+                       _name = package.get_config ("identity");
 
                if (name == null) {
                        var name = file.get_basename ();
@@ -41,90 +42,6 @@ private class Games.LoveGame : Object, Game {
                }
        }
 
-       private string? parse_package (string path) {
-               Archive.Read archive = new Archive.Read ();
-
-               archive.support_filter_all ();
-               archive.support_format_all ();
-
-               var result = archive.open_filename(path, BLOCK_SIZE);
-               if (result != Archive.Result.OK)
-                       return null; // FIXME: Should throw error.
-
-               var is_valid = false;
-               string? config_file = null;
-
-               weak Archive.Entry entry;
-               while(archive.next_header(out entry) == Archive.Result.OK) {
-                       var file_path = entry.pathname ();
-                       switch (file_path) {
-                       case "main.lua":
-                               is_valid = true;
-
-                               break;
-                       case "conf.lua":
-                               config_file = read_config_file (archive);
-
-                               break;
-                       }
-               }
-
-               if (!is_valid)
-                       return null; // FIXME: Should throw error.
-
-               return config_file;
-       }
-
-       private string read_config_file (Archive.Read archive) {
-               string config = "";
-
-               char buffer[BLOCK_SIZE];
-               while (archive.read_data (buffer, BLOCK_SIZE) != 0)
-                       config += (string) buffer;
-
-               return config;
-       }
-
-       private void parse_config_file (string config_file) {
-               var regex = /^\s*[^\s]+\.([^\s\.]+)\s*=\s*(.+?)\s*$/;
-
-               var config = new HashTable<string, string> (GLib.str_hash, GLib.str_equal);
-
-               var lines = config_file.split ("\n");
-               MatchInfo match_info;
-               foreach (var line in lines)
-                       if (regex.match (line, RegexMatchFlags.ANCHORED, out match_info)) {
-                               var key = match_info.fetch (1);
-                               var lua_value = match_info.fetch (2);
-                               config[key] = lua_value;
-                       }
-
-               if (name == null && config.contains ("title")) {
-                       var real_value = parse_string (config["title"]);
-                       if (real_value != null)
-                               _name = real_value;
-               }
-
-               if (name == null && config.contains ("identity")) {
-                       var real_value = parse_string (config["identity"]);
-                       if (real_value != null)
-                               _name = real_value;
-               }
-       }
-
-       private string? parse_string (string lua_value) {
-               if (lua_value.length < 2)
-                       return null;
-
-               if (!lua_value.has_prefix ("\""))
-                       return null;
-
-               if (!lua_value.has_suffix ("\""))
-                       return null;
-
-               return lua_value[1:-1];
-       }
-
        public Runner get_runner () throws Error {
                string[] args = { "love", path };
 
diff --git a/plugins/love/src/love-package.vala b/plugins/love/src/love-package.vala
new file mode 100644
index 0000000..6bf2b29
--- /dev/null
+++ b/plugins/love/src/love-package.vala
@@ -0,0 +1,146 @@
+// This file is part of GNOME Games. License: GPLv3
+
+private class Games.LovePackage : Object {
+       private const size_t BLOCK_SIZE = 4096;
+
+       private string uri;
+       private HashTable<string, string> config;
+
+       public LovePackage (string uri) throws Error {
+               this.uri = uri;
+
+               if (!contains_file ("main.lua"))
+                       throw new LoveError.INVALID_PACKAGE ("This doesn't represent a valid LÖVE package: 
'%s'.", uri);
+
+               var config_file = get_file_string ("conf.lua");
+               if (config_file == null)
+                       throw new LoveError.INVALID_PACKAGE ("This doesn't represent a valid LÖVE package: 
'%s'.", uri);
+
+               var regex = /^\s*[^\s]+\.([^\s\.]+)\s*=\s*(.+?)\s*$/;
+
+               config = new HashTable<string, string> (GLib.str_hash, GLib.str_equal);
+
+               var lines = config_file.split ("\n");
+               MatchInfo match_info;
+               foreach (var line in lines)
+                       if (regex.match (line, RegexMatchFlags.ANCHORED, out match_info)) {
+                               var key = match_info.fetch (1);
+                               var lua_value = match_info.fetch (2);
+                               config[key] = lua_value;
+                       }
+       }
+
+       public string? get_config (string key) {
+               if (!config.contains (key))
+                       return null;
+
+               return parse_string (config[key]);
+       }
+
+       public bool contains_file (string path_in_archive) {
+               var file = File.new_for_uri (uri);
+               var path = file.get_path ();
+
+               Archive.Read archive = new Archive.Read ();
+
+               archive.support_filter_all ();
+               archive.support_format_all ();
+
+               var result = archive.open_filename(path, BLOCK_SIZE);
+               if (result != Archive.Result.OK)
+                       return false;
+
+               weak Archive.Entry entry;
+               while(archive.next_header(out entry) == Archive.Result.OK) {
+                       var file_path = entry.pathname ();
+                       if (file_path != path_in_archive)
+                               continue;
+
+                       return true;
+               }
+
+               return false;
+       }
+
+       public InputStream? get_file_input_stream (string path_in_archive) {
+               var file = File.new_for_uri (uri);
+               var path = file.get_path ();
+
+               Archive.Read archive = new Archive.Read ();
+
+               archive.support_filter_all ();
+               archive.support_format_all ();
+
+               var result = archive.open_filename(path, BLOCK_SIZE);
+               if (result != Archive.Result.OK)
+                       return null;
+
+               weak Archive.Entry entry;
+               while(archive.next_header(out entry) == Archive.Result.OK) {
+                       var file_path = entry.pathname ();
+                       if (file_path != path_in_archive)
+                               continue;
+
+                       var size = entry.size ();
+
+                       return read_file_to_input_stream (archive, size);
+               }
+
+               return null;
+       }
+
+       public string? get_file_string (string path_in_archive) {
+               var file = File.new_for_uri (uri);
+               var path = file.get_path ();
+
+               Archive.Read archive = new Archive.Read ();
+
+               archive.support_filter_all ();
+               archive.support_format_all ();
+
+               var result = archive.open_filename(path, BLOCK_SIZE);
+               if (result != Archive.Result.OK)
+                       return null;
+
+               weak Archive.Entry entry;
+               while(archive.next_header(out entry) == Archive.Result.OK) {
+                       var file_path = entry.pathname ();
+                       if (file_path != path_in_archive)
+                               continue;
+
+                       return read_file_to_string (archive);
+               }
+
+               return null;
+       }
+
+       private InputStream read_file_to_input_stream (Archive.Read archive, int64 size) {
+               uint8[] content = new uint8[size];
+               archive.read_data (content, (size_t) size);
+
+               return new MemoryInputStream.from_data (content);
+       }
+
+       private string read_file_to_string (Archive.Read archive) {
+               string content = "";
+
+               char buffer[BLOCK_SIZE];
+               while (archive.read_data (buffer, BLOCK_SIZE) != 0)
+                       content += (string) buffer;
+
+               return content;
+       }
+
+       private string? parse_string (string lua_value) {
+               if (lua_value.length < 2)
+                       return null;
+
+               if (!lua_value.has_prefix ("\""))
+                       return null;
+
+               if (!lua_value.has_suffix ("\""))
+                       return null;
+
+               return lua_value[1:-1];
+       }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]