Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Last active August 29, 2015 14:14
Show Gist options
  • Save timsgardner/7bf4a604427797dae821 to your computer and use it in GitHub Desktop.
Save timsgardner/7bf4a604427797dae821 to your computer and use it in GitHub Desktop.
def-compget
(defmacro condcast-> [expr xsym & clauses]
(let [exprsym (gensym "exprsym_")
[clauses default] (if (even? (count clauses))
[clauses nil]
[(butlast clauses)
[:else
`(let [~xsym ~exprsym]
~(last clauses))]])
cs (->> clauses
(partition 2)
(mapcat
(fn [[t then]]
`[(instance? ~t ~exprsym)
(let [~(with-meta xsym {:tag t}) ~exprsym]
~then)])))]
`(let [~exprsym ~expr]
~(cons 'cond
(concat cs default)))))
;; working on it
(defmacro gc [gob t]
(let [gobsym (with-meta gob {:tag GameObject})] ;; yeh
`(let [~gobsym ~gob
^UnityEngine.Component c# (.GetComponent ~gobsym (~'type-args ~t))]
(when-not (destroyed? c#) c#))))
(defmacro def-compget [name typesym]
(let [objsym (gensym "obj_")]
`(defn ~name ~(with-meta [objsym] {:tag typesym})
(condcast-> ~objsym ~objsym
~typesym ~objsym
UnityEngine.GameObject (gc ~objsym ~typesym)
UnityEngine.Component (let [^UnityEngine.GameObject obj2# (.gameObject ~objsym)]
(gc obj2# ~typesym))))))
;; (def-compget transform-getit UnityEngine.Transform)
;; (transform-getit (objects-typed UnityEngine.GameObject))
;; => #<Transform Game Object (UnityEngine.Transform)>
@timsgardner
Copy link
Author

actually kind of redundant with get-component

@timsgardner
Copy link
Author

Mrm, not as redundant as it should be. I should incorporate similar code into get-component. Here's the disassembly of (def-compget transform UnityEngine.Transform):

    //
    // Static Fields
    //
    protected internal static Var const__3;

    protected internal static object const__4;

    protected internal static object const__2;

    protected internal static Var const__0;

    protected internal static object const__1;

    //
    // Constructors
    //
    static urbanOutfitters/moholy$transform__265 ()
    {
        urbanOutfitters/moholy$transform__265.const__0 = RT.var ("clojure.core", "instance?");
        urbanOutfitters/moholy$transform__265.const__1 = (object)RT.classForName ("UnityEngine.Transform");
        urbanOutfitters/moholy$transform__265.const__2 = (object)RT.classForName ("UnityEngine.GameObject");
        urbanOutfitters/moholy$transform__265.const__3 = RT.var ("arcadia.core", "destroyed?");
        urbanOutfitters/moholy$transform__265.const__4 = (object)RT.classForName ("UnityEngine.Component");
    }

    //
    // Methods
    //
    public override bool HasArity (int num)
    {
        return num == 1;
    }

    public override object invoke (object obj)
    {
        object arg_FB_0;
        if (obj is Transform)
        {
            arg_FB_0 = obj;
        }
        else
        {
            if (obj is GameObject)
            {
                object component = ((GameObject)obj).GetComponent<Transform> ();
                object obj2;
                arg_FB_0 = (((obj2 = ((IFn)urbanOutfitters/moholy$transform__265.const__3.getRawRoot ()).invoke (component)) == null || (obj2 is bool && !(bool)obj2)) ? component : null);
            }
            else
            {
                if (obj is Component)
                {
                    object gameObject = ((Component)obj).gameObject;
                    object obj3 = gameObject;
                    object component2 = ((GameObject)obj3).GetComponent<Transform> ();
                    object obj4;
                    arg_FB_0 = (((obj4 = ((IFn)urbanOutfitters/moholy$transform__265.const__3.getRawRoot ()).invoke (component2)) == null || (obj4 is bool && !(bool)obj4)) ? component2 : null);
                }
                else
                {
                    arg_FB_0 = null;
                }
            }
        }
        return arg_FB_0;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment