Browse Source

Merge pull request #893 from mrpeidz/unity_webgl_plugin

Add Unity WebGL plugin
Justin Mayer 7 years ago
parent
commit
13af1c0382

+ 2 - 0
Readme.rst

@@ -262,6 +262,8 @@ Twitter Bootstrap         Defines some rst directive that enable a clean usage o
 
 
 txt2tags_reader           Reader that renders txt2tags markup in content
 txt2tags_reader           Reader that renders txt2tags markup in content
 
 
+Unity WebGL               Easily embed Unity3d games into posts and pages
+
 Video Privacy Enhancer    Increases user privacy by stopping YouTube, Google, et al from placing cookies via embedded video
 Video Privacy Enhancer    Increases user privacy by stopping YouTube, Google, et al from placing cookies via embedded video
 
 
 W3C validate              Submits generated HTML content to the W3C Markup Validation Service
 W3C validate              Submits generated HTML content to the W3C Markup Validation Service

+ 18 - 0
pelican_unity_webgl/LICENSE.txt

@@ -0,0 +1,18 @@
+Copyright (c) 2017 Mr.Page
+Permission is hereby granted, free of charge, to any person obtaininga copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 81 - 0
pelican_unity_webgl/README.rst

@@ -0,0 +1,81 @@
+===================
+Pelican Unity WebGL
+===================
+
+Compatibility
+=============
+
+Unity >= 5.6 (Tested on 5.6)
+
+Usage
+=====
+
+Basic directive
+---------------
+
+.. code-block:: rst
+
+	.. unitywebgl GAME_ID
+
+**GAME_ID** is the required parameter. This is name of the directory, that contains your webgl build. This directory should be place in path, specified in config.py file, or with :gameroot: parameter.
+
+Optional parameters
+-------------------
+
+.. code-block:: rst
+
+	.. unitywebgl GAME_ID
+		:gameroot: /games/new/
+		:template: /games/templates/newtemplate
+		:width: 640
+		:height: 480
+
++-------------------+------------------+---------------------------------------------------------+
+| Parameter         | default value    |                                                         |
++===================+==================+=========================================================+
+| gameroot          | /games           | path to directory with games                            |
++-------------------+------------------+---------------------------------------------------------+
+| templatepath      | /games/utemplate | path to template                                        |
++-------------------+------------------+---------------------------------------------------------+
+| width             |                                                                            |
++-------------------+ Player resolution                                                          |
+| height            |                                                                            |
++-------------------+------------------+---------------------------------------------------------+
+
+.. note::
+	Test pelican project can be found in *pelican_demo.zip* archive `here <https://github.com/mrpeidz/Unity-WebGL-RST-directive>`_
+
+Configuration
+=============
+
+You can change default root directory, template path and player resolution in the *config.py* file.
+
+Modifying html output template
+------------------------------
+
+You can modify the *template.txt* file to change html output.
+
+Template parameters explanation:
+
++-------------------+-------------------------------+
+| Parameter         | default value                 |
++===================+===============================+
+| 0                 | game directory name           |
++-------------------+-------------------------------+
+| 1                 | directory, where games placed |
++-------------------+-------------------------------+
+| 2                 | template directory            |
++-------------------+-------------------------------+
+| 3                 | width                         |
++-------------------+-------------------------------+
+| 4                 | height                        |
++-------------------+-------------------------------+
+
+About
+=======
+
+License: `MIT <https://opensource.org/licenses/MIT>`_
+
+`Unity WebGL RST Directive github page <https://github.com/mrpeidz/Unity-WebGL-RST-directive>`_
+
+**Mr.Page (c) 2017**

+ 86 - 0
pelican_unity_webgl/UnityGameDirective.py

@@ -0,0 +1,86 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2017 Mr.Page
+# Permission is hereby granted, free of charge, to any person obtaininga copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+from __future__ import unicode_literals
+
+from docutils import nodes
+from docutils.parsers.rst import directives, Directive
+
+from . import config
+
+import os
+
+
+class UnityWebgl(Directive):
+    required_arguments = 1
+    optional_arguments = 4
+
+    option_spec = {
+        'width': directives.positive_int,
+        'height': directives.positive_int,
+        'gameroot': directives.unchanged_required,
+        'template': directives.unchanged_required,
+    }
+
+    final_argument_whitespace = False
+    has_content = False
+
+    def load_template(self, game, gamesroot, templatepath, width, height):
+        basepath = os.path.dirname(__file__)
+        filepath = os.path.abspath(os.path.join(basepath, 'template.txt'))
+        with open(filepath, 'r') as template:
+            data = template.read()
+            return data.format(game, gamesroot, templatepath, width, height)
+
+    def run(self):
+
+        # load config
+
+        game = self.arguments[0].strip()
+        games_path = config.GAMES_ROOT_DIR
+        template_path = config.TEMPLATE_PATH
+        width = config.DEFAULT_WIDTH
+        height = config.DEFAULT_HEIGHT
+
+        # get params
+
+        if 'width' in self.options:
+            width = self.options['width']
+        if 'height' in self.options:
+            height = self.options['height']
+        if 'gameroot' in self.options:
+            games_path = self.options['gameroot']
+        if 'template' in self.options:
+            template_path = self.options['template']
+
+        # remove slashes
+
+        games_path = games_path.rstrip('/')
+        template_path = template_path.rstrip('/')
+
+        html = self.load_template(game, games_path, template_path, width, height)
+
+        return [
+            nodes.raw('', html, format='html')]
+
+
+def register():
+    directives.register_directive('unitywebgl', UnityWebgl)

+ 3 - 0
pelican_unity_webgl/__init__.py

@@ -0,0 +1,3 @@
+# -*- coding: utf-8 -*-
+
+from .UnityGameDirective import register

+ 10 - 0
pelican_unity_webgl/config.py

@@ -0,0 +1,10 @@
+# unity webgl options
+
+DEFAULT_WIDTH = 960
+DEFAULT_HEIGHT = 600
+DEFAULT_ALIGN = 'center'
+
+# paths
+
+GAMES_ROOT_DIR = '/games'  # directory with games
+TEMPLATE_PATH = '/games/utemplate'  # template path

+ 9 - 0
pelican_unity_webgl/template.txt

@@ -0,0 +1,9 @@
+<!-- Begin Unity webgl app -->
+    <link rel="stylesheet" href="{2}/style.css">
+    <script src="{2}/UnityProgress.js"></script>
+    <script src="{1}/{0}/Build/UnityLoader.js"></script>
+    <script>
+        var gameInstance = UnityLoader.instantiate("gameContainer", "{1}/{0}/Build/{0}.json", {{onProgress: UnityProgress}});
+    </script>
+    <div id="gameContainer" style="width: {3}px; height: {4}px; left: 50%; transform: translateX(-50%);"></div>
+<!-- End Unity webgl app -->

+ 69 - 0
pelican_unity_webgl/test_unitywebgl.py

@@ -0,0 +1,69 @@
+from unittest import TestCase
+from docutils.core import publish_string
+from docutils.parsers.rst import directives
+
+from unitywebgl import config
+from unitywebgl.UnityGameDirective import UnityWebgl
+
+class TestUnityWebgl(TestCase):
+
+    def test_directive_basic(self):
+
+        # test directive html output with default template
+        # and settings
+
+        directives.register_directive('unitywebgl', UnityWebgl)
+
+        config.GAMES_ROOT_DIR = '/test_games_root'
+        config.TEMPLATE_PATH = '/test_template_path/template'
+        config.DEFAULT_WIDTH = 960
+        config.DEFAULT_HEIGHT = 600
+
+        html =['<link rel="stylesheet" href="/test_template_path/template/style.css">',
+        '<script src="/test_template_path/template/UnityProgress.js">',
+        '<script src="/test_games_root/testgame/Build/UnityLoader.js">',
+        '<script>\n        var gameInstance = UnityLoader.instantiate("gameContainer", "/test_games_root/testgame/Build/testgame.json", {onProgress: UnityProgress});\n    </script>',
+        '<div id="gameContainer" style="width: 960px; height: 600px; left: 50%; transform: translateX(-50%);"></div>']
+
+        res = publish_string('.. unitywebgl:: testgame', writer_name='html', settings_overrides={'output_encoding': 'unicode'})
+
+        passed = True
+
+        for line in html:
+            if line not in res:
+                passed = False
+                break
+
+        assert passed
+
+
+    def test_directive_with_params(self):
+
+        # test directive html output with all optional parameters,
+        # default template and settings
+
+        directives.register_directive('unitywebgl', UnityWebgl)
+
+        config.GAMES_ROOT_DIR = 'test_games_root'
+        config.TEMPLATE_PATH = 'test_template_path'
+        config.DEFAULT_WIDTH = 960
+        config.DEFAULT_HEIGHT = 600
+
+        html =['<link rel="stylesheet" href="/games2/template2/style.css">',
+        '<script src="/games2/template2/UnityProgress.js">',
+        '<script src="/games2/testgame/Build/UnityLoader.js">',
+        '<script>\n        var gameInstance = UnityLoader.instantiate("gameContainer", "/games2/testgame/Build/testgame.json", {onProgress: UnityProgress});\n    </script>',
+        '<div id="gameContainer" style="width: 640px; height: 480px; left: 50%; transform: translateX(-50%);"></div>']
+
+        d = '.. unitywebgl:: testgame\n\t:gameroot: /games2\n\t:template: /games2/template2\n\t:width: 640\n\t:height: 480'
+
+        res = publish_string(d, writer_name='html', settings_overrides={'output_encoding': 'unicode'})
+
+        passed = True
+
+        for line in html:
+            if line not in res:
+                passed = False
+                break
+
+        assert passed